It is narrated from Sayyiduna Abu Hurayrah (may Allah be pleased with him) that the Prophet (peace and blessings be upon him) said: "A jinn called an 'ifrit began to harass me last night in order to disrupt my prayer. Allah Almighty gave me power over him. I wanted to tie him to one of the pillars of the mosque so that in the morning you could all see him, but I remembered the supplication of my brother Sulayman (peace be upon him) which he made: 'My Lord, forgive me and grant me a kingdom such as will not belong to anyone after me. Indeed, You are the Bestower' [Surah Sad: 35]. So I let him go, disgraced."
Hadith Referenceالسنن الكبرى للبيهقي / كتاب الصلاة / 3184