Do Major Sins Erase Good Deeds in Islam?
Question

Does Allah accept the good deeds of adulterous people or not? Does adultery make good deeds profitless? Can an adulterer who keeps on doing this evil action, have good deeds, or they will not be accepted until he repents to Allah? Does Allah accept the fasting and prayers of an adulterer? Knowing that the prophet, peace be upon him said what means that the doors of heavens open after midnight and a caller calls saying, will anyone pray so that his prayers will be accepted, will anyone ask for something so that it will be given to him, is there anyone in distress so that Allah relieves it, then there would be no Muslim asking Allah for anything except that it will be given to him, except an adulterer who earns by his genitals, and a man who takes taxes unfairly. It is mentioned in the above Hadith also that Allah will make their good deeds like scattered floating particles of dust, because they commit what Allah forbade. Please clarify this matter and how authentic is this Hadith?
Praise be to Allah.What the Quran Says About Major Sins and Good Deeds
One of the well-established facts that is affirmed by the Quran and on which Ahl As-Sunnah 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 disbelief and polytheism. This is indicated by the verse in which Allah 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.} [Al-Baqarah 2:217]
Views of Ahl As-Sunnah on Major Sins and Good Deeds
Shaykh Al-Islam Ibn Taymiyah (may Allah have mercy on him) said in Majmu` Al-Fatawa (10/321-322):
The Companions and Ahl As-Sunnah wal-Jama`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 As-Sunnah. Nothing erases all good deeds except disbelief, 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 Allah, Allah will reward him for that, even if he deserves to be punished for his major sin. The Book of Allah distinguishes between the ruling on thieves, adulterers and believers fighting with one another, and the ruling on the disbelievers with regard to how they are labelled and the ruling applicable to them. The Mutawatir Sunnah of the Prophet (peace and blessings of Allah be upon him) and the consensus of the Companions points to that. According to Ahl As-Sunnah wal-Jama`ah, good deeds are accepted from the one who fears Allah and does them purely for the sake of Allah and in accordance with the command of Allah. 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)
Does Committing a Major Sin Erase All Good Deeds?
The Hadith which is mentioned in the question is the Hadith narrated by Thawban (may Allah be pleased with him) from the Prophet (peace and blessings of Allah 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 Tihamah, but Allah will make them like scattered dust.” Thawban said: O Messenger of Allah, 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 Allah.” (Narrated by Ibn Majah in his Sunan, 4245; Ar-Rawiyani in Al-Musnad, 1/425; At-Tabarani in Al-Awsat, 5/46 and Al-Saghir, 1/396; Musnad Al-Shamiyin,  no. 667; Ad-Daylami in Musnad Al-Firdaws, 7715. Classed as authentic by Al-Albani in As-Silsilah As-Sahihah, 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.
With regard to the second Hadith you mentioned, it is the Hadith of `Uthman ibn Abu Al-`As Ath-Thaqafi (may Allah be pleased with him), according to which the Prophet (peace and blessings of Allah 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 Allah will answer him, except for an adulteress who earns a living from committing adultery or an extortionist.” (Narrated by At-Tabarani in Al-Mu`jam Al-Kabir, 9/59, and in Al-Mu`jam Al-Awsat, 3/154. Al-Haythami said in Majma` Az-Zawa’id, (10/156): Its men are the men of Sahih. Al-Albani said in As-Silsilah As-Sahihah, 91073): Its chain of narration is authentic.)
Based on this, there is no suggestion that all his good deeds will be erased, rather it shows that the Du`a of the adulterer who is persisting in committing adultery  will not be accepted, and this is a valid meaning. Major sins are one of the impediments to Du`a being accepted. How can Allah respond to one who is remaining in sin and not giving it up or repenting from it?
And Allah knows best.