Praise be to Allaah.
The scholars have stated that the reason why it is mustahabb to seek refuge with Allaah when entering the bathroom is to seek protection with Him from the devils who frequent places where there are impurities and where ‘awrahs are uncovered. There are ahaadeeth which indicate that.
It was narrated from Zayd ibn Arqam (may Allaah be pleased with him) that the Messenger of Allaah (peace and blessings of Allaah be upon him) said: “These toilets are haunted (inhabited by devils), so when anyone of you goes to the toilet let him say, ‘A’oodhu Billaahi min al-khubthi wa’l-khabaa’ith (I seek refuge Allaah from the male and female devils).” Narrated by Abu Dawood, 6; classed as saheeh by al-Albaani in Saheeh Abi Dawood.
Al-Hattaab said: These places are singled out for seeking protection with Allaah for two reasons:
1. Because they are toilets, and by Allaah’s will the devils dominate toilets.
2. The toilet is a filthy place where one should not verbally remember Allaah, so the shaytaan takes advantage if one does not remember Him, because remembrance of Allaah drives him away. So the command to seek refuge with Allaah before that means that one will be protected from the shaytaan until he comes out. End quote.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) said in al-Sharh al-Mumti’ (1/83):
The purpose of this seeking refuge is to seek protection with Allaah against al-khubthi wa’l-khabaa’ith (the male and female devils) because this is an evil and unclean place (khabeeth), and the place that is khabeeth is the abode of al-khubthi wa’l-khabaa’ith (the male and female devils). So it is appropriate when wanting to enter the toilet to say: A’oodhu Billaahi min al-khubthi wa’l-khabaa’ith (I seek refuge Allaah from the male and female devils), so that no khubth (evil) or khabaa’ith (evil spirits) will affect him. End quote.
This reason dictates that the Muslim should always seek refuge with Allaah every time he enters the toilet, whether he intends to relieve himself or he is entering for some other purpose for which people use bathrooms nowadays, such as cleaning and so on. Thus the Muslim will protect himself from the harm of the devils.
It says in al-Mughni (1/190):
Ahmad said: He should say when entering the toilet: A’oodhu Billaahi min al-khubthi wa’l-khabaa’ith (I seek refuge Allaah from the male and female devils). I never entered the place for doing wudoo’ without saying it but something I disliked happened to me. End quote.
Some fuqaha’ have stated something similar, as it says in Haashiyat Nihaayat al-Muhtaaj min Furoo’ al-Shaafa’i (1/142):
When taking a child into the toilet so that the child can relieve himself, is it Sunnah for him to say it on behalf of the child or not? There are different opinions concerning that but it is not unlikely that he should say it.
That also includes the mother wanting to breastfeed her child in the place where he relieves himself, or sitting him on the potty. End quote.
Shaykh Ibn Baaz (may Allaah have mercy on him) was asked in Majmoo’ al-Fataawa (10/29): Is the du’aa’ only for entering the bathroom, or is it only when a person wants to relieve himself?
He replied that it is mustahabb to say the du’aa’ for entering the bathroom in all cases, without limiting it to when a person is going to relieve himself.
The same applies to the du’aa’ for leaving the washroom. Al-Tirmidhi (7) narrated that ‘Aa’ishah said: When the Prophet (peace and blessings of Allaah be upon him) exited the toilet, he used to say Ghufraanaka (Your forgiveness).
The scholars have stated that the reason why one should seek forgiveness when exiting the toilet is that it is for not remembering Allaah whilst one was in the toilet. The Muslim regards that as a shortcoming and makes up for it by asking for forgiveness.
See al-Nihaayah fi Ghareeb al-Hadeeth by Ibn al-Atheer (3/703).
This wisdom also applies to the one who enters the toilet for a reason other than relieving himself. Seeking forgiveness is the daily habit of the Muslim, so there is nothing wrong with saying it after coming out of the bathroom.
See also the answer to question no. 26816.
And Allaah knows best.