Praise be to Allah.

If a person commits sin knowing that he is disobeying Allah and going against His command and exposing himself to punishment, then he repents sincerely, Allah will accept his repentance and forgive him, no matter how great his sin was. Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) said:

If a person repents from sin, Allah will forgive him, whether it was shirk or otherwise, as He says (interpretation of the meaning):

“Say: O ‘Ibaadi (My slaves) who have transgressed against themselves (by committing evil deeds and sins)! Despair not of the Mercy of Allaah, verily, Allaah forgives all sins. Truly, He is Oft‑Forgiving, Most Merciful”

[al-Zumar 39:53].

Ibn al-Qayyim (may Allah have mercy on him) said:

If a person repents sincerely, his previous bad deeds will be erased and the reward for his good deeds will be restored for him.

End quote from al-Waabil as-Sayyib, p. 12

Secondly:

With regard to the words of Allah (interpretation of the meaning):

“Allah accepts only the repentance of those who do evil in ignorance and foolishness and repent soon afterwards; it is they to whom Allah will forgive and Allah is Ever All-Knower, All-Wise”

[an-Nisa’ 4:17],

what is meant by ignorance in this verse is deciding to go ahead and commit an act of disobedience towards Allah; it does not mean that he does not know in the first place that what he is doing is a sin and act of disobedience. In this case, if he repents soon afterwards, Allah will accept his repentance. Everyone who repents before death is regarded as having repented “soon afterwards”. This is by the grace of Allah and His abundant mercy to His slaves.

Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) said:

Abu’l-‘Aaliyah said: I asked the companions of Muhammad about this verse and they said to me: Everyone who disobeys Allah is ignorant, and everyone who repents before death has repented “soon afterwards”. This was also the view of the majority of commentators.

Mujaahid said: Everyone who disobeys Allah is ignorant at the time of his disobedience. Al-Hasan, Qataadah, ‘Ata’, as-Suddi and others said: They are only called ignorant because of their disobedience, not because they do not have the power of discernment.

Az-Zajjaaj said: The verse does not mean that they are ignorant of it being bad, because if a Muslim does something the nature of which he is unaware of, he is regarded as being like one who did not do anything wrong. Rather what is meant here is one of two things: either they did it when they were not quite aware of its bad consequences or they did it knowingly and were aware that its consequences would be bad, but they preferred immediate worldly gain to the Hereafter, so they were called ignorant because of their preference for a little worldly pleasure over abundant pleasure and eternal well-being (in the Hereafter).

What is meant here is that everyone who disobeys Allah is ignorant, and everyone who fears Him has knowledge.

End quote from Majmoo‘ al-Fataawa, 7/22

See also Tafseer Ibn Katheer, 2/235

The scholars of the (Standing) Committee were asked:

I am an old man, fifty-nine years old, and I was afraid when I heard the verse in which Allah, may He be exalted, says (interpretation of the meaning):

“Allah accepts only the repentance of those who do evil in ignorance and foolishness and repent soon afterwards; it is they to whom Allah will forgive and Allah is Ever All-Knower, All-Wise.

And of no effect is the repentance of those who continue to do evil deeds until death faces one of them and he says: Now I repent”

[an-Nisa’ 4:17-18].

Does committing bad deeds knowing that they are bad prevent one from repenting?

The scholars of the Committee replied:

The fact that a person committed a sin knowing that it was a sin does not prevent him from repenting. There are three conditions for repentance: giving up the sin, regretting what one has done, and resolving not to go back to the sin. If the rights of any human beings were affected, then it is essential to restore them to him or to ask him to let one off. And everyone who disobeys Allah is ignorant.

End quote from Fataawa al-Lajnah ad-Daa’imah, 24/308-309

Thirdly:

The verse (interpretation of the meaning):

“And those who, when they have committed Fahishah (illegal sexual intercourse etc.) or wronged themselves with evil, remember Allah and ask forgiveness for their sins; - and none can forgive sins but Allah - and do not persist in what (wrong) they have done, while they know”

[Aal ‘Imraan 3:135]

does not contradict what we have said above. What this verse means is that one of the characteristics of the pious slaves of Allah is that, if they commit any serious major sin or any sin less than that, they hasten to repent and ask for forgiveness, and they remember their Lord and what He has warned the sinners of and what He has promised to the pious; so they ask Him to forgive their sins and conceal their faults, whilst also giving them up and regretting them. Hence He says “and do not persist in what (wrong) they have done, while they know.”

See: Tafseer as-Sa‘di, p. 148

Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) said:

If he persists in a minor sin it becomes a major sin, but if he repents from it, he will be forgiven. … If he repents sincerely, his sins will be forgiven. Then if he goes back to the sin, he had to repent again, and if he repents Allah will accept his repentance again.

End quote from Majmoo‘ al-Fataawa, 11/700

For more information on the topic of repentance, please see the answer to question no. 46683

And Allah knows best.