Praise be to Allah.
Compulsive waswaas (OCD) is a kind of sickness, and the one who is affected by it should treat himself in a number of ways:
1 – Turning to Allah and beseeching Him, for He answers the one who is in distress if he calls upon Him, and grants him relief. So, let him say what Ayyoob (peace be upon him) said:
And [mention] Job, when he called to his Lord, "Indeed, adversity has touched me, and you are the Most Merciful of the merciful." (21:83)
And the Prophet (peace and blessings of Allah be upon him) said: “Allah has not sent down any disease but He has also sent down its cure. Those who know it, know it, and those who do not know it, do not know it.” Narrated by Ahmad, classed as hasan (sound) by al-Albani in Ghayat al-Maram. So the cure comes from Him, and healing is in His Hand, so beseech Allah and make the most of the time before dawn, for Allah descends every night to the lowest heaven when the last third of the night is left, and He says: “Who will call upon Me, that I may answer him? Who will ask of Me, that I may give to him? Who will seek My forgiveness, that I may forgive him?” Narrated al-Bukhari, and Muslim.
2 – Turning away from waswas and what it calls you to, and distracting yourself from it by keeping busy with worship and acts of obedience as much as possible. Hence the Prophet (peace and blessings of Allah be upon him) told the one who was affected by waswas: “Let him seek refuge with Allah and stop (such thoughts).” Narrated by al-Bukhari, and Muslim,.
If the urge to swear and curse comes, then seek refuge with Allah from the accursed shaytan, and keep yourself busy with remembrance of Allah and glorifying Him, or raise your voice reciting Quran, or speak to your friend, and so on.
3 – Remember Allah a great deal (dhikr), especially by praying for forgiveness, for waswas comes from the shaytan or he has some involvement in it, but when Alah is mentioned he slinks away or flees. Allah says (interpretation of the meaning):
And if there comes to you from Satan an evil suggestion, then seek refuge in Allah . Indeed, He is the Hearing, the Knowing. (41:36)
Ibn Katheer (may Allah have mercy on him) said in his Tafseer (exegesis):
With regard to the devil among the jinn, there is no way to ward it off if he whispers except to seek refuge with his Creator Who has allowed him to gain power over you. If you seek refuge with Allah and turn to Him, He will restrain him and foil his plot. When the Messenger of Allah (peace and blessings of Allah be upon him) stood up to pray he would say: “I seek refuge with Allah, the All-Hearing, All-Knowing, from the accursed shaytan, from his whisper, his arrogance and his poetry.” End quote.
Muslim narrated that ‘Uthman ibn Abi’l-‘As came to the Prophet (peace and blessings of Allah be upon him) and said: “O Messenger of Allah, the shaytan has interfered with my prayer and made my recitation confused.” The Messenger of Allah (peace and blessings of Allah be upon him) said: “That is a devil called Khanzab. If you experience that, then seek refuge with Allah from him, and spit drily to your left three times.” He said: I did that, and Allah took it away from me.
And the advice that Yahya (peace be upon him) gave to his companions was: “I enjoin you to remember Allah, for the likeness of that is of a man who was pursued by the enemy, until he came to a strong fortress where he found protection from them. Similarly, a person cannot protect himself from the shaytan except by remembering Allah.” Narrated by al-Tirmidhi, classed as saheeh (authentic) by al-Albani in Saheeh al-Tirmidhi.
4 – Going to a trustworthy Muslim doctor, for this waswas is a kind of sickness, as stated above. The Prophet (peace and blessings of Allah be upon him) said: “Seek medical treatment, for Allah has not created any disease but He has also created a cure for it, except for one disease: old age.” Narrated by Ahmad, Abu Dawood, al-Tirmidhi, 2038; Ibn Majah, classed as saheeh by al-Albani in Saheeh Abi Dawood.
5 – You should note that whoever is afflicted with this sickness is excused for what he says, because he does not say it willingly and he does not like it. Rather he will be rewarded, in sha Allah, if he bears it with patience and seeks reward. He will be rewarded for hating the waswas and trying to escape it. Hence when some of the Companions of the Prophet (peace and blessings of Allah be upon him) came and asked him: “We find in ourselves something that is too awful for any of us to speak of it,” he said: “Do you really find that?” They said: Yes. He said: “That is clear faith.” Narrated by Muslim, from the hadeeth of Abu Hurayrah (may Allah be pleased with him).
Al-Nawawi said in Sharh Muslim:
What this means is that the fact that you find it too awful to speak of is a sign of clear faith, because finding it too awful to speak of and being afraid of it and of uttering it, let alone believing it, is the sign of one who has achieved true and perfect faith, and who has shed all doubts. End quote.
Please see also questions no. 62839, 25778 and 39684
We ask Allah to heal you and make you well.
And Allah knows best.