Do Bad Deeds Erase Good Deeds in Islam?
Question

What are the bad deeds that cancel out good deeds if a person does them and cause them to be erased from the record of deeds?
Praise be to Allah.Disbelief and its Impact on Good Deeds
One of the basic principles among Ahl As-Sunnah wal-Jama`ah is that good deeds are not accepted if they are accompanied by disbelief, and that nothing cancels out all good deeds except disbelief.
This is indicated by the verse in which Allah says (interpretation of the meaning):
{Say: Spend (in the Cause of Allah) willingly or unwillingly, it will not be accepted from you. Verily, you are ever a people who are Fasiqun (rebellious, disobedient to Allah).}
{And nothing prevents their contributions from being accepted from them except that they disbelieved in Allah and in His Messenger (Muhammad), and that they came not to As-Salah (the prayer) except in a lazy state, and that they offer not contributions but unwillingly.} [At-Tawbah 9:53-54]
Ibn Taymiyah (may Allah have mercy on him) said:
Nothing cancels out good deeds except disbelief, because the one who dies as a believer will inevitably enter Paradise, and he will be brought out of Hell if he does enter it, but if all his good deeds are cancelled out, he will never enter Paradise. Good deeds can only be cancelled out by that which is their opposite, and nothing can be the opposite of all good deeds except disbelief. This is well known from the principles of Sunnah. (End quote from  As-Sarim Al-Maslul, p. 55)
The followers of innovation, such as the Khawarij, Mu`tazilah and Murji’ah differed. The Khawarij and Mu`tazilah went to extremes and said that major sins erase and cancel out all good deeds and acts of worship. In contrast, the Murji’ah said that the good deed of faith cancels out all bad deeds.
Do Sins Erase Good Deeds in Islam?
Once it is understood that nothing can cancel out all good deeds except that which cancels out faith altogether, namely disbelief, then can some sins cancel out and erase some good deeds?
Shaykh Al-Islam Ibn Taymiyah (may Allah have mercy on him) said in Majmu` Al-Fatawa (10/638):
If bad deeds do not cancel out all good deeds, can they cancel out a proportional amount of them, or can some good deeds be erased by sin that is less than disbelief?
There are two views among those who claim adherence to the Sunnah, some who deny that and some who affirm it. End quote.

The first view is that bad deeds do not cancel out good deeds, rather good deeds are the ones that cancel out bad deeds, by the grace and bounty and kindness of Allah.

Al-Qurtubi (may Allah have mercy on him) said in Al-Jami` li Ahkam Al-Quran (3/295):
The correct `Aqidah is that bad deeds do not cancel out good deeds. (End quote)

The second view is that sins and innovations may cancel out the reward for good deeds as a form of punishment. Shaykh Al-Islam Ibn Taymiyah attributed this view in Majmu` Al-Fatawa (10/322) to the majority of Ahl As-Sunnah.

This was also the view favored by Shaykh Al-Islam and his student Ibn Al-Qayyim. He said in Madarij As-Salikin (1/278):
Ahmad stated that in one report and said: A person should get married if he fears for himself; he should borrow money and get married, lest he fall into haram actions which would cancel out his good deeds. (End quote)
The Fear of Hypocrisy and Losing Good Deeds
Imam Al-Bukhari (may Allah have mercy on him) said in Kitab Al-Iman (the Book of Faith) in his Sahih:
Chapter: Fear of the believer inadvertently cancelling out his good deeds. Ibrahim At-Taymi (may Allah have mercy on him) said: When I compare my words with my deeds, I am afraid that my deeds belie my words. Ibn Abu Mulaykah said: I met thirty of the companions of the Prophet (peace and blessings of Allah be upon him) all of whom feared that they might be guilty of hypocrisy, and not one of them said that his faith was like that of Jibreel and Mika’eel. It was narrated from Al-Hasan: No one fears it (hypocrisy) but a believer and no one feels safe from it but a hypocrite. And one should be afraid of persisting in hypocrisy and sin without repenting , because Allah says (interpretation of the meaning): {and [they] do not persist in what (wrong) they have done, while they know.} [Al `Imran 3:135]
Imam Muslim also entitled a chapter: The believer’s fear of his good deeds being cancelled out.
Imam Ibn Rajab (may Allah have mercy on him) said:
Al-Bukhari’s title for this chapter is suited to what is mentioned in it of good deeds being cancelled out by some sins, as Allah says (interpretation of the meaning):
{O you who believe! Raise not your voices above the voice of the Prophet, nor speak aloud to him in talk as you speak aloud to one another, lest your deeds should be rendered fruitless while you perceive not.} [Al-Hujurat 49:2]
Imam Ahmad said: Al-Hasan ibn Musa said: Hammad ibn Salamah narrated from Habib ibn Ash-Shahid, that Al-Hasan said: These people do not think that some deeds can cancel out others, but Allah says (interpretation of the meaning):
{O you who believe! Raise not your voices …, lest your deeds should be rendered fruitless while you perceive not,} [Al-Hujurat 49:2]
This is also indicated by the verses in which Allah says (interpretation of the meaning):
{O you who believe! Do not render in vain your Sadaqah (charity) by reminders of your generosity or by injury, like him who spends his wealth to be seen of men, and he does not believe in Allah, nor in the Last Day. His likeness is the likeness of a smooth rock on which is a little dust; on it falls heavy rain which leaves it bare. They are not able to do anything with what they have earned. And Allah does not guide the disbelieving people.} [Al-Baqarah 2:264]
{Would any of you wish to have a garden with date palms and vines, with rivers flowing underneath, and all kinds of fruits for him therein, while he is stricken with old age, and his children are weak (not able to look after themselves), then it is struck with a fiery whirlwind, so that it is burnt? Thus does Allah make clear His Ayat (proofs, evidences, verses) to you that you may give thought.} [Al-Baqarah 2:266]
Al-Bukhari  (may Allah have mercy on him) narrated that `Umar (may Allah be pleased with him) asked the people about it and they said: Allah knows best. Ibn `Abbas said: The verse gives a likeness of deeds. ‘Umar said: What deeds? Ibn `Abbas said: Deeds. ‘Umar said: A rich man may strive in obedience of Allah, then Allah sends the shaytan to him and he commits sins until they drown out his good deeds.
`Ata’ Al-Khursani (may Allah have mercy on him) said: This is a man whose deeds end with polytheism or a major sin , so all his good deeds are cancelled out.
It is narrated in an authentic report that the Prophet (peace and blessings of Allah be upon him) said: “Whoever does not pray `Asr, his deeds are cancelled out.” (Narrated by Al-Bukhari, 553)
It also says in As-Sahih that a man said: By Allah, Allah will not forgive So and so. Allah said: ‘Who is the one who swore by Me that I will not forgive So and so? I have forgiven so and so and I have cancelled out your good deeds. (Narrated by Muslim, 2621)
`Aishah (may Allah be pleased with her) said: Tell Zayd that his Jihad with the Messenger of Allah (peace and blessings of Allah be upon him) will be cancelled out unless he repents. (Narrated by Ad-Daraqutni, 3/52 and Al-Bayhaqi, 5/330)
This indicates that some bad deeds cancel out some good deeds, but they may be restored if one repents.
Ibn Abi Hatim narrated in his Tafsir from Abu Ja`far, from Ar-Rabi` ibn Anas, that Abu Al-`Aliyah said: The companions of the Messenger of Allah (peace and blessings of Allah be upon him) thought that no sin could do harm if one was sincere and that no good deed was of any avail if one associates others with Allah. Then Allah revealed the words (interpretation of the meaning):
{O you who believe! Obey Allah, and obey the Messenger (Muhammad) and render not vain your deeds.} [Muhammad 47:33]
Then they began to fear major sins after they found out that they may cancel out good deeds.
It was narrated from Al-Hasan that he said concerning the words “and render not vain your deeds” i.e., by committing sins . It was narrated from Ma`mar from Az-Zuhri concerning the words “and render not vain your deeds” i.e., by committing major sins.
It was narrated that Qatadah said concerning this verse: Whoever among you can avoid rendering vain his righteous deeds by doing bad deeds, let him do so, and there is no strength except with Allah. Good cancels out evil, and evil cancels out good, and what matters is one’s final deeds.
Ibn Rajab (may Allah have mercy on him) said: There are very many reports from the Salaf about good deeds being cancelled out by major sins, and it would take too long to list them all. Hudhayfah said: Slandering a chaste innocent woman cancels out the good deeds of a hundred years.
It was narrated that `Ata’ said: A man may say a word in anger that destroys the good deeds of sixty or seventy years.
Imam Ahmad said, according to the report of Al-Fadl ibn Ziyad: No one of you can be sure that he will not steal a glance that will cancel out his good deeds.
As for those who say that the view that good deeds may be cancelled out by bad deeds is the view of the Khawarij and Mu`tazilah, this is a false view, and the views of the righteous Salaf do not agree with that. Yes, the Khawarij and Mu`tazilah said that faith is cancelled out by major sins and that major sin dooms one to spend eternity in Hell, but this is a false view which was held by them alone. (Sharh Kitab Al-Iman min Sahih Al-Bukhari, 206-210)
Ibn Al-Qayyim said: The things that cancel out or spoil good deeds are too many to count. It is not the deeds that count, rather it is the protection of one’s good deeds from that which may spoil them or cancel them out. (Al-Wabil As-Sayyib, 18).
And Allah knows best.