Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
quran:12:42

And he said to the one whom he knew would go free, "Mention me before your master." But Satan made him forget the mention ˹to˺ his master, and Joseph remained in prison several years.  

Then he said to the one whom he deemed, ˹whom˺ he was certain, would be saved of the two — and this was the cup-bearer: ‘Mention me to your lord’, your master, and say to him, ‘There is a young man in jail who has been wrongly imprisoned’. So he ˹the cup-bearer˺ departed, but Satan caused him, that is, the cupbearer, to forget the mention, of Joseph, before his master, so that he, Joseph, stayed, he remained, in prison for some years: it is said ˹that he remained˺ for seven years, or for twelve.
القرآن:١٢:٤٢

وَقَالَ لِلَّذِي ظَنَّ أَنَّهُ نَاجٍ مِنْهُمَا اذْكُرْنِي عِنْدَ رَبِّكَ فَأَنْسَاهُ الشَّيْطَانُ ذِكْرَ رَبِّهِ فَلَبِثَ فِي السِّجْنِ بِضْعَ سِنِينَ  

{وقال للذي ظن} أيقن {أنه ناج منهما} وهو الساقي {اذكرني عند ربك} سيدك فقل له إن في السجن غلاما محبوسا ظلما فخرج {فأنساه} أي الساقي {الشيطان ذكْرَ} يوسف عند {ربه فلبث} مكث يوسف {في السجن بضع سنين} قيل سبعا وقيل اثنتي عشرة.