The temptation to commit a sin is a disease like the sin itself. Like all other diseases, protection against this sin involves two stages e.g. prevention and cure.
When it comes to the prevention stage, it should be noted that a person can eradicate all those things that may lead him to a sin by strengthening his relations with God and invoking the Ahlul-Bait (a). In the same way, he can resist the temptation to commit sins by taking the evil consequences of sinning into consideration.
As for curing the temptations, we can also say that curing the temptations are easy insofar as they have not turned into action; because in this stage there are the divine angels who with reliance on man's monotheistic nature and reason invite him towards purity and cleanness. If a person really seeks God's help at this stage and turns to the Prophet and his household (peace be upon them), he will overpower the temptations.
Moreover, the supplications and prayers which the imams (divine leaders) – peace and benedictions of Allah be upon them – have taught their companions are useful in this regard. These supplications are mentioned in the supplication books. Late Shaykh Abbas Qommi have mentioned some of them in his book, Mafatihul Jenan:
1. Hadith Nafs prayer: It has been narrated from Imam Sadiq (a) that there is no momeen on whom forty days pass except that he goes through a state of hadith nafs. When he enters the state of hadith nafs, he should offer two Rak'ats of prayers and seek refuge in God. Prophet Adam complained to God about hadith nafs and Gabriel came down and said: "Say La hawla wa la quwata illa billah. He recited this zikr and hadith nafs was removed from him.
It has been narrated that in order for a person to prevent temptations and he feel a doubt in his heart, he should recite:
هو الاول و الاخر و الظاهر و الباطن و هو بکل شیء علیم
"Huwal awwalu wal akhiru waz-Zahiru wal-Batin wa huwa bekulli shay'in alim."
"He is the First and the Last and the Ascendant (over all) and the Knower of hidden things, and He is Cognizant of all things."
2. Du'a (supplication) for warding off satanic temptations.
3. Du'a for warding off the evil of iblis.
4. Invocation and seeking divine help and refuge in God.
For further information, see: Index: Warding off Bad Thoughts and Bad Memories, question 3525 (site: 3755).