Praise be to Allaah.
person prays then realizes that he was junub, he must do ghusl and repeat
the prayer; his first ghusl does not count that he did as a habit or to keep
clean, without the intention to remove janaabah, because the Prophet
(peace and blessings of Allaah be upon him) said: “Actions are but by
intentions and each person will have but that which he intended.” Narrated
by al-Bukhaari (1) and Muslim (1907).
Qudaamah (may Allaah have mercy on him) said in al-Mughni (1/158): If
he intended to do an act of purification for something for which
purification is not prescribed, such as to cool down, and he did not intend
the purification that is prescribed in sharee’ah, then the impurity is not
removed because he did not have the intention of purification, so it does
not count as anything.