One of the well-established facts that is affirmed by the Qur’aan and on which Ahl al-Sunnah are unanimously agreed is that sins and major sins do not erase all of the good deeds of the Muslim, and that there is nothing that erases the good deeds of a Muslim altogether except kufr and shirk. This is indicated by the verse in which Allaah says (interpretation of the meaning):
“And whosoever of you turns back from his religion and dies as a disbeliever, then his deeds will be lost in this life and in the Hereafter, and they will be the dwellers of the Fire. They will abide therein forever”
Shaykh al-Islam Ibn Taymiyah (may Allaah have mercy on him) said in Majmoo’ al-Fataawa (10/321-322):
The Sahaabah and Ahl al-Sunnah wa’l-Jamaa’ah are of the view that those who commit major sins will be brought forth from Hell and intercession will be made for them. A single major sin does not erase all good deeds, but it may erase an equivalent amount of them, according to most of Ahl al-Sunnah. Nothing erases all good deeds except kufr, just as nothing erases all bad deeds except repentance. If the one who has committed a major sin does some good deeds, seeking thereby the pleasure of Allaah, Allaah will reward him for that, even if he deserves to be punished for his major sin. The Book of Allaah distinguishes between the ruling on thieves, adulterers and believers fighting with one another, and the ruling on the kuffaar with regard to how they are labelled and the ruling applicable to them. The mutawaatir Sunnah of the Prophet (peace and blessings of Allaah be upon him) and the consensus of the Sahaabah points to that. According to Ahl al-Sunnah wa’l-Jamaa’ah, good deeds are accepted from the one who fears Allaah and does them purely for the sake of Allaah and in accordance with the command of Allaah. If a person is sincere towards Him in doing an action, He will accept it from him, even if he disobeys Him in other ways; if a person is not sincere towards Him when doing it, He will not accept it from him, even if he obeys Him in other ways. End quote.
The hadeeth which is mentioned in the question is the hadeeth narrated by Thawbaan (may Allaah be pleased with him) from the Prophet (peace and blessings of Allaah be upon him) who said: “I certainly know people of my ummah who will come on the Day of Resurrection with good deeds like the mountains of Tihaamah, but Allaah will make them like scattered dust.” Thawbaan said: O Messenger of Allaah, describe them to us and tell us more, so that we will not become of them unknowingly. He said: “They are your brothers and from your race, worshipping at night as you do, but they will be people who, when they are alone, transgress the sacred limits of Allaah.”
Narrated by Ibn Majaah in his Sunan (no .4245); al-Rawiyaani in al-Musnad (1/425); al-Tabaraani in al-Awsat (5/46) and al-Sagheer (1/396); Musnad al-Shaamiyeen (no. 667); al-Daylami in Musnad al-Firdaws (7715). Classed as saheeh by al-Albaani in al-Silsilah al-Saheehah (505).
This is evidence that supports the principle we have mentioned, which is that some bad deeds may cancel out a portion of a person’s good deeds and of the reward for his righteous needs.
For more information on what we have mentioned about this issue, please see the answer to question no. 81874.
With regard to the second hadeeth you mentioned, it is the hadeeth of ‘Uthmaan ibn Abi’l-‘Aas al-Thaqafi, according to which the Prophet (peace and blessings of Allaah be upon him) said:
“The gates of heaven are opened halfway through the night and a caller cries out: Is anyone supplicating so that he may be answered? Is anyone asking so that he might be given? Is anyone in distress, so that he might be granted relief? And there will be no Muslim left who is supplicating but Allaah will answer him, except for a zaaniyah who earns a living from committing zina or an extortionist.”
Narrated by al-Tabaraani in al-Mu’jam al-Kabeer (9/59) and in al-Mu’jam al-Awsat (3/154). Al-Haythami said in Majma’ al-Zawaa’id (10/156): Its men are the men of saheeh. Al-Albaani said in al-Silsilah al-Saheehah 91073): Its isnaad is saheeh.
Based on this, there is no suggestion that all his good deeds will be erased, rather it shows that the du’aa’ of the adulterer (zaani) who is persisting in his zina will not be accepted, and this is a valid meaning. Major sins are one of the impediments to du’aa’ being accepted. How can Allaah respond to one who is remaining in sin and not giving it up or repenting from it?
And Allaah knows best.