What if married couples didnt know that anal sex is haram and kept doing it from years will Allah forgive them.There are many people who doesn't know that so will Allah forgive them for that ?
First of all Allah says:
... And when they have purified themselves, then come to them from where Allah has ordained for you. (2:222)
This verse is regarded as an evidence from the qur'an that anal intercourse is haram. And in the sunnah you may read:
The Prophet (ﷺ) said: He who has intercourse with his wife through her anus is accursed. (Sunan abi Dawod)
“Allah will not look at a man who has intercourse with his wife in her buttocks.” (Sunan ibn Majah)
Assuming the wife was aware of this rule she is held to hinder her husband from doing so -if she can-, as she doesn't need to obey a husband who wants her to do something Allah hates:
Nevertheless it might happen that people do this due to lack of knowledge in this case applies:
... And there is no blame upon you for that in which you have erred but [only for] what your hearts intended. And ever is Allah Forgiving and Merciful. (33:5)
Verily Allah has pardoned [or been lenient with] for me my ummah: their mistakes, their forgetfulness, and that which they have been forced to do under duress (See in the 40 hadiths of an-Nawawi or in Sunan ibn Majah and Sunan al-Bayhaqi)
This means that both must repent sincerely and never do this again once they learnt about the truth.
See also -in Arabic- fatwa islamweb #101842