Short answer
In the Hanafi school it depends. If such doubt is rare for you, start the prayer again. If it happens often, go with what you think is most likely; if you have no leaning either way, take the lower number, then do sajdah al-sahw. Doubts after the salam are ignored.
If doubt about the number of rakats is not a habit, the prayer is restarted. If it happens often, you act on your strongest assumption, and if neither side is stronger you build on the lower number, sitting for tashahhud wherever the rakat could be the last, and do sajdah al-sahw. Doubt after finishing the prayer is disregarded.
Other madhhabs
- Shafi'i
- Always build on certainty, which is the lower number, then do sajdah al-sahw before the salam; there is no restarting.
- Maliki
- Build on the lower number and do sajdah al-sahw.
- Hanbali
- The usual position is to build on the lower number; one narration lets the imam act on his strongest assumption.
Explanation
There are two hadith on this. One says to throw away the doubt and build on what you are certain of. The other says to aim for what is right (your best judgement) and complete the prayer on that. The Hanafi school uses both, depending on the person's situation:
- Rare doubt (it is not something that usually happens to you): end the prayer and start again. This is the safest way for someone whose prayer is normally clear.
- Frequent doubt, with a leaning: go with what you think is most likely, for example "I think this is the third", and carry on. Do sajdah al-sahw at the end.
- Frequent doubt, no leaning at all: take the lower number. Because the rakat might be the last, sit for tashahhud after it as well, then continue, and do sajdah al-sahw at the end.
After the salam: once you have finished, a doubt about the prayer is ignored, unless you are certain you missed something. Behind an imam: follow the imam; your doubt does not matter.
If doubts come in almost every prayer and cause you distress, this may be waswasa (intrusive thoughts). The rule then is to ignore them, not to keep repeating, and a scholar who knows you can guide you. The method of sahw is in our guide to sajdah sahw; see also forgetting a rakah.
Ask a mufti about your own case. This answer explains the general ruling. When the details of your situation matter, as they do here, ask a qualified mufti and give them the full facts. Where to ask a scholar.
The evidence
Quran, Surah Al-Baqara 2:286
لَا يُكَلِّفُ ٱللَّهُ نَفْسًا إِلَّا وُسْعَهَا ۚ لَهَا مَا كَسَبَتْ وَعَلَيْهَا مَا ٱكْتَسَبَتْ ۗ رَبَّنَا لَا تُؤَاخِذْنَآ إِن نَّسِينَآ أَوْ أَخْطَأْنَا ۚ رَبَّنَا وَلَا تَحْمِلْ عَلَيْنَآ إِصْرًۭا كَمَا حَمَلْتَهُۥ عَلَى ٱلَّذِينَ مِن قَبْلِنَا ۚ رَبَّنَا وَلَا تُحَمِّلْنَا مَا لَا طَاقَةَ لَنَا بِهِۦ ۖ وَٱعْفُ عَنَّا وَٱغْفِرْ لَنَا وَٱرْحَمْنَآ ۚ أَنتَ مَوْلَىٰنَا فَٱنصُرْنَا عَلَى ٱلْقَوْمِ ٱلْكَٰفِرِينَ ﴿٢٨٦﴾
Allah does not charge a soul except [with that within] its capacity. It will have [the consequence of] what [good] it has gained, and it will bear [the consequence of] what [evil] it has earned. "Our Lord, do not impose blame upon us if we have forgotten or erred. Our Lord, and lay not upon us a burden like that which You laid upon those before us. Our Lord, and burden us not with that which we have no ability to bear. And pardon us; and forgive us; and have mercy upon us. You are our protector, so give us victory over the disbelieving people."
The rules for doubt keep prayer within what a person can bear.
Sahih Muslim 571
Sahih... فليطرح الشك وليبن على ما استيقن ...
"Let him throw away the doubt and build on what he is certain of."
Building on certainty when there is no clear leaning.
Sahih al-Bukhari 401
Sahih... فليتحر الصواب فليتم عليه ...
"Let him aim for what is correct and complete his prayer on that basis."
Acting on one's best judgement.
Where the ruling is recorded
- al-Hidaya, Kitab al-Salah, chapter on sajdah al-sahw. If doubt is the first time he restarts; if it is frequent he acts on his strongest assumption, otherwise on certainty.
- al-Durr al-Mukhtar with Radd al-Muhtar, Chapter on sajdah al-sahw, doubt in prayer. Doubt after the salam is not considered, and 'first time' is explained as not being his habit.
This answer is pending review by the scholars on our team. Quran translation by Sahih International. Hadith Arabic from the standard editions of the six books and the Muwatta, with gradings as recorded there; the English is our own rendering. Spotted a mistake? Tell us.