Islam’s solution to prevent the repetition of sin
Given the ugliness of sin and the fact that the thought of it is worrisome, what causes one to repeat the perpetration of certain sins? What is the solution of Islam to prevent the recurrence of sin?
The Holy Quran, by giving insight to man, wants to inform him of the dangers and harms of sin that hinder happiness, and to awaken his pure and divine innate nature, so that a spirit of avoiding sin may appear in him. In this regard, he says: “…Eat of what God has provided you and do not follow in Satan’s footsteps; he is indeed your manifest enemy.”  It is understood from this verse that the awakened innate nature of man requires that man is sitting at the table of the Lord of the Worlds and is making use of His blessings, he should not obey his enemy and abandon obedience to God.
If man follows the steps of Satan and prefers his desires of his ego and lusts to the command of the shari’ah, which prefers by reason and narration, this sinful act causes man to be captured by the desires of his ego, then man sins knowingly and intentionally and will no longer be able to forsake sin. He will no longer be able to forsake sin. Because the captive has no will of his own and cannot carry out his will, the wisest thing to do is not to let one’s intellect become entrapped from the beginning by committing sins and following Satan. Imam Ali said: “Many a slavish mind is subservient to overpowering longings.”  Therefore, whoever fears the position of his Lord and keeps his soul separated from his ego, surely Paradise is his abode.  Despite the esoteric desires for the domination over one’s ego and desires, the captivity of reason and man’s neglect of the remembrance of God causes the repetition of sin.
Ways to prevent the recurrence of sin
it is narrated from the Safinah al-Najah (the Ship of Salvation) of humankind, Imam al-Husayn, that once a person approached him and asked him for help. He said: “I am a disobedient man. I cannot hold out against sinning.” Imam al-Husayn said: “Do these five things, then commit whatever sin you want. Eat from other than the sustenance that God has provided you; Then commit whatever sin you want. Be independent of the support and protection of God; Then commit whatever sin you want. Choose a place where God cannot see you; Then commit whatever sin you want. When the Angel of Death comes to take your soul, turn him away; Then commit whatever sin you want. When the angels who guard the hellfire want to throw you in, don’t go; Then commit whatever sin you want.” 
If a person pays a little attention and realizes that he is eating from God’s sustenance and everything that he has – from the limbs, organs and physical and external blessing – is all from God, and wherever he goes, he is in the kingdom of God and wherever he is, he is in the presence of the Lord of the Worlds and He is the witness and observer of his actions. (“Does he not know that God sees [him]?)”  And he should be aware that man must die in any position and rank that he is in and he has no authority and will against the will of the Lord of the Worlds and the Angel of Death and he should know that if he is a sinner, he will be thrown into the fire whether he wants to be or not. Heaven and earth cannot stand it and take these truths in the depths of their being and make them a habit. Whenever he wants to commit a sin, these facts are reviewed for him and prevent him from committing the sin. Loss of attachment to the world is a major factor in abandoning sin. In a narration, the love of the world is considered the basis of every sin and sins are watered by the fountain of worldly love. Not having attachment to the world also means that we do not make it the goal of our work and do not use our efforts for worldly life but to make the world a tool for the prosperity of our eternal life and the development of our human personality. A little of it does not make us depressed and sad and a lot of it does not make us revolt.
Imam Ali said: “Beware of disobeying God in solitude, because the witness (of that situation) is also the judge.” 
There are many works in the books of narrations about sin, the mention of which is effective in abandoning sin. The effects of sins in this world, the effects of sins in the hereafter, the effects of sins on the descent of tribulations, the effects of sins on the cutting off sustenance, the effects of sins on the cruelty of the heart, etc. For example, we narrate a narration for a good ending, Imam Ja’far al-Sadiq said: “No vein will be cut, no feet will be hit by a stone, no one will have a headache unless we commit sin and this is the promise of God Almighty in his book, which says: ‘Whatever affliction that may visit you is because of what your hands have earned, and He excuses many [an offense of yours].’  What God forgives is much more than he asks about” 
Some ways to stop oneself from sinning:
• Screening out of bad friends
• Choosing spiritual friends and accompanying them
• Making use of the works of pious and moral scholars and their pilgrimages
• Pilgrimage to holy places
• Daily recitation of the Quran
• Immediate return from sins
• Use a self-reform program, including: setting out setting out conditions to do good deeds and avoid bad deed every morning, daily meditation, nightly observation of one’s deeds and inspection of their good and bad deeds, encouraging oneself to do good deeds and punishment for bad deeds
 كُلُوا مِمَّا رَزَقَكُمُ اللَّـهُ وَلَا تَتَّبِعُوا خُطُوَاتِ الشَّيْطَانِ ۚ إِنَّهُ لَكُمْ عَدُوٌّ مُّبِينٌ
Holy Quran 6:142
 وكم من عقل أسير تحت هوى أمير
Nahj al-Balaghah, hadith number 211
 فَالسَّابِقَاتِ سَبْقًا
“By those who, racing, take the lead”
Holy Quran 79:4
فَإِنَّ الْجَنَّةَ هِيَ الْمَأْوَىٰ
“His refuge will indeed be paradise.”
Holy Quran 79:41
 عنه عليه السلام ـ لِرجُلٍ قالَ لَهُ : أنا رجُلٌ عاصٍ و لا أصبِرُ عَنِ المَعصِيَةِ ، فَعِظْني بِمَوعِظَةٍ ـ : اِفعَلْ خَمسَةَ أشياءَ و أذنِبْ ما شِئتَ ، فأوَّلُ ذلكَ : لا تَأكُلْ رِزقَ اللّه ِ و أذنِبْ ما شِئتَ ، و الثّانِي : اُخرُجْ مِن وَلايَةِ اللّه ِ و أذنِبْ ما شِئتَ ، و الثّالِثُ : اُطلُبْ مَوضِعا لا يَراكَ اللّه ُ و أذْنِبْ ما شِئتَ ، و الرّابِعُ : إذا جاءَ مَلَكُ المَوتِ لِيَقبِضَ رُوحَكَ فادْفَعْهُ عَن نَفسِكَ و أذنِبْ ما شِئتَ ، و الخامِسُ : إذا أدخَلَكَ مالِكٌ في النّارٍ فلا تَدخُلْ في النّارِ و أذنِبْ ما شِئتَ!
Bihar al-Anwar, vol. 78, p. 126, narration 7, chapter 20
 أَلَمْ يَعْلَم بِأَنَّ اللَّـهَ يَرَىٰ
Holy Quran 96:14
 اتَّقُوا مَعَاصِيَ اللهِ فِي الْخَلَوَاتِ، فَإِنَّ الشَّاهِدَ هُوَ الْحَاكِمُ
Nahj al-Balaghah, hadith number 324
 وَمَا أَصَابَكُم مِّن مُّصِيبَةٍ فَبِمَا كَسَبَتْ أَيْدِيكُمْ وَيَعْفُو عَن كَثِيرٍ
Holy Quran 42:30
 عَنْهُ عَنْ أَبِيهِ عَنِ اَلنَّضْرِ بْنِ سُوَيْدٍ عَنْ هِشَامِ بْنِ سَالِمٍ عَنْ أَبِي عَبْدِ اَللَّهِ عَلَيْهِ اَلسَّلاَمُ قَالَ: أَمَا إِنَّهُ لَيْسَ مِنْ عِرْقٍ يَضْرِبُ وَ لاَ نَكْبَةٍ وَ لاَ صُدَاعٍ وَ لاَ مَرَضٍ إِلاَّ بِذَنْبٍ وَ ذَلِكَ قَوْلُ اَللَّهِ عَزَّ وَ جَلَّ فِي كِتَابِهِ : «وَ مٰا أَصٰابَكُمْ مِنْ مُصِيبَةٍ فَبِمٰا كَسَبَتْ أَيْدِيكُمْ وَ يَعْفُوا عَنْ كَثِيرٍ » قَالَ ثُمَّ قَالَ وَ مَا يَعْفُو اَللَّهُ أَكْثَرُ مِمَّا يُؤَاخِذُ بِهِ
Usul al-Kafi, vol. 2, p. 269, narration 3