Anas ibn Mālik (may Allah be pleased with him) reported: The Messenger of Allah (may Allah’s peace and blessings be upon him) said: "Spitting in the mosque is a sin and its expiation is to bury it.”
شرح الحديث :
Spitting on the floor of the mosque or on its walls is a sin, and the person who does it deserves the punishment of Allah, the Exalted. It is not permissible for a Muslim to spit in the mosque, no matter what the circumstances are. It is disrespectful to the House of Allah, and it makes it dirty. We are ordered to keep the mosque clean and to treat it with respect. Allah says in the Qur’an: ﴿And whoever honors the sacred ordinances of Allah - it is best for him in the sight of his Lord﴾ [Sūrat al-Hajj: 30]. There is no harm in spitting in one’s garment, turban, or in a tissue, as the prohibition does not address them. If someone were to spit on the floor of the mosque by mistake, he would not be held accountable for this and his sin would be forgiven. It does not mean that one can spit on the floor intentionally then bury it with dirt. This is because the Messenger (may Allah's peace and blessings be upon him) stated that merely spitting in the mosque is a sin. This explanation of the Hadīth is further strengthened by the Hadīth in Bukhāri and Muslim in which the Messenger of Allah (may Allah’s peace and blessings upon him) "saw mucus on the wall of the mosque, so he stood up and scratched it off with his hand.” If anyone spits on the floor of the mosque accidentally without intending to do so and wants Allah to forgive him, he should quickly remove it by burying it if the floor is made of dirt. If, however, there is carpet on the floor of the mosque, he should wipe it off until it is no longer visible. If, however, he is unable to remove it, he will be sinful. Abu Dharr reported that the Messenger of Allah (may Allah’s peace and blessings be upon him) said: "I was shown the actions of my nation, both good and bad, and I found one of their good deeds is removing harmful things from the road. I found one of the bad deeds is spit in the mosque that is not buried." [Muslim]