He did ghusl with the intention of doing it for Jumu‘ah and he forgot about doing it for janaabah; did it remove his impurity?
It is better for the one who is in a state of janaabah to hasten to do ghusl lest he forget. This has been discussed previously in the answer to question no. 20847
If a person does ghusl for Jumu‘ah and forgets about being in a state of major impurity, his impurity is removed, according to the correct scholarly opinion. Al-Bahooti (may Allah have mercy on him) said: If he intends to do a Sunnah ghusl, such as ghusl for Jumu‘ah or ‘Eid, it suffices for an obligatory ghusl such as in the case of janaabah and so on, if he forgot about the impurity which made it obligatory.
End quote from Kashshaaf al-Qinaa‘, 1/89.
Al-Hajjaawi said in Zaad al-Mustaqni‘: If he intended to do a Sunnah ghusl, it suffices for an obligatory one. End quote.
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said: For example, if he does ghusl after washing a deceased person or he does ghusl for entering ihraam or for standing in ‘Arafah, these are Sunnah ghusls. The same applies to ghusl for Jumu‘ah according to the majority of scholars.
The apparent meaning of the author’s words (al-Hajjaawi) – which is our madhhab – is: If he remembers that he had to do an obligatory ghusl, some of our companions limited it to the case where he forgot that he was in a state of impurity, i.e., he forgot about being in a state of janaabah. If he had not forgotten that then it does not remove the impurity, because the Sunnah ghusl is not done to remove impurity, and if it was not done to remove impurity, the Prophet (blessings and peace of Allah be upon him) said: “Actions are but by intentions,” and this man only intended to do the Sunnah ghusl and he knew that he was in a state of janaabah and was aware of that, so how could it remove the impurity?
This opinion – which restricts it to cases where one forgot – is valid.
The basis for this view is that because the Sunnah ghusl is an act of purification that is prescribed in sharee‘ah, it does remove the impurity. But this explanation is somewhat flawed, because there is no doubt that it is a ghusl that is prescribed in sharee‘ah, but it is of a lesser standing that obligatory ghusl for janaabah, so how can a Sunnah action be strong enough to suffice for an obligatory action that is of a higher standing?
But if he had forgotten, then he is excused.
For example, if he did ghusl for Jumu‘ah – on the basis that it is Sunnah – and he was in a state of janaabah but did not remember that, or he did not realise that he had been in a state of janaabah until after the prayer, such as if he had a wet dream and did not realise until after the prayer, then his Jumu‘ah prayer is valid because the impurity was removed.
But if he was aware of it and he intended to do the Sunnah ghusl only, then the view that it suffices is not quite certain.
End quote from ash-Sharh al-Mumti‘, 1/201
Based on that, your ghusl was valid and did remove the janaabah, and your prayer was valid.
And Allah knows best.