كتاب الحج
15  
The Book of Pilgrimage
(88)
Chapter: Al-Madinah eliminates its Dross and it is also called Tabah, and Taibah
(88)
باب الْمَدِينَةِ تَنْفِي شِرَارَهَا ‏‏
Sahih Muslim 1383

Jabir b. `Abdullah (Allah be pleased with them) reported that a desert Arab swore allegiance to Allah's Messenger (ﷺ). He suffered from a severe fever in Medina (and) so he came to Allah's Messenger (ﷺ) saying:

Muhammad, cancel my oath of allegiance. But Allah's Messenger (ﷺ) refused it. He again came and said: Cancel my oath of allegiance. But he (the Holy Prophet) refused it. He again came to him and said: Cancel my oath of allegiance, but he refused. The desert Arab, however, went away (cancelling the allegiance himself). Thereupon Allah's Messenger (ﷺ) said: Medina is like a furnace which drives away its impurity and purifies what is good.
حَدَّثَنَا يَحْيَى بْنُ يَحْيَى، قَالَ قَرَأْتُ عَلَى مَالِكٍ عَنْ مُحَمَّدِ بْنِ الْمُنْكَدِرِ، عَنْ جَابِرِ، بْنِ عَبْدِ اللَّهِ أَنَّ أَعْرَابِيًّا، بَايَعَ رَسُولَ اللَّهِ صلى الله عليه وسلم فَأَصَابَ الأَعْرَابِيَّ وَعَكٌ بِالْمَدِينَةِ فَأَتَى النَّبِيَّ صلى الله عليه وسلم فَقَالَ يَا مُحَمَّدُ أَقِلْنِي بَيْعَتِي ‏.‏ فَأَبَى رَسُولُ اللَّهِ صلى الله عليه وسلم ثُمَّ جَاءَهُ فَقَالَ أَقِلْنِي بَيْعَتِي ‏.‏ فَأَبَى ثُمَّ جَاءَهُ فَقَالَ أَقِلْنِي بَيْعَتِي ‏.‏ فَأَبَى فَخَرَجَ الأَعْرَابِيُّ فَقَالَ رَسُولُ اللَّهِ صلى الله عليه وسلم ‏ "‏ إِنَّمَا الْمَدِينَةُ كَالْكِيرِ تَنْفِي خَبَثَهَا وَيَنْصَعُ طَيِّبُهَا ‏"‏ ‏.‏
Reference : Sahih Muslim 1383
In-book reference : Book 15, Hadith 559
USC-MSA web (English) reference : Book 7, Hadith 3191
  (deprecated numbering scheme)