Index: mythmusic/mythmusic/main.cpp
===================================================================
--- mythmusic/mythmusic/main.cpp	(revision 12582)
+++ mythmusic/mythmusic/main.cpp	(working copy)
@@ -245,6 +245,34 @@
     }
 }
 
+void cleanDB()
+{
+    MythProgressDialog *clean_progress;
+    clean_progress = new MythProgressDialog(
+	QObject::tr("Cleaning music database"), 3);
+
+    int counter = 0;
+
+    MSqlQuery query(MSqlQuery::InitCon());
+    query.exec("DELETE FROM music_genres " 
+               "WHERE genre_id not in " 
+               "(SELECT DISTINCT genre_id FROM music_songs);"); 
+    clean_progress->setProgress(++counter);
+
+    query.exec("DELETE FROM music_albums "
+	       "WHERE album_id not in "
+	       "(SELECT DISTINCT album_id FROM music_songs);");
+    clean_progress->setProgress(++counter);
+
+    query.exec("DELETE FROM music_artists "
+	       "WHERE artist_id not in "
+	       "(SELECT DISTINCT artist_id FROM music_songs);");
+    clean_progress->setProgress(++counter);
+
+    clean_progress->Close();
+    delete clean_progress; 
+}
+
 void SearchDir(QString &directory)
 {
     MusicLoadedMap music_files;
@@ -313,20 +341,28 @@
        3) UpdateFileInDB, same as 1.
      */
 
+    bool files_removed = false;
     counter = 0;
     for (iter = music_files.begin(); iter != music_files.end(); iter++)
     {
         if (*iter == kFileSystem)
             AddFileToDB(iter.key());
         else if (*iter == kDatabase)
+	{
             RemoveFileFromDB(directory, iter.key ());
+	    files_removed = true;
+	}
         else if (*iter == kNeedUpdate)
             UpdateFileInDB(iter.key());
 
         file_checking->setProgress(++counter);
     }
+
     file_checking->Close();
     delete file_checking;
+
+    if(files_removed)
+      cleanDB();
 }
 
 void startPlayback(PlaylistsContainer *all_playlists, AllMusic *all_music)
