private static void deleteOrphanedRecords() {
        Log.d("Database clean: removing non-existing lists");
        database.delete(dbTableCachesLists, "list_id <> " + StoredList.STANDARD_LIST_ID + " AND list_id NOT IN (SELECT _id + " + customListIdOffset + " FROM " + dbTableLists + ")", null);

        Log.d("Database clean: removing non-existing caches from attributes");
        database.delete(dbTableAttributes, "geocode NOT IN (SELECT geocode FROM " + dbTableCaches + ")", null);

        Log.d("Database clean: removing non-existing caches from spoilers");
        database.delete(dbTableSpoilers, "geocode NOT IN (SELECT geocode FROM " + dbTableCaches + ")", null);

        Log.d("Database clean: removing non-existing caches from lists");
        database.delete(dbTableCachesLists, "geocode NOT IN (SELECT geocode FROM " + dbTableCaches + ")", null);

        Log.d("Database clean: removing non-existing caches from waypoints");
        database.delete(dbTableWaypoints, "geocode NOT IN (SELECT geocode FROM " + dbTableCaches + ")", null);

        Log.d("Database clean: removing non-existing caches from variables");
        database.delete(dbTableVariables, "geocode NOT IN (SELECT geocode FROM " + dbTableCaches + ")", null);

        Log.d("Database clean: removing non-existing caches from categories");
        database.delete(dbTableCategories, "geocode NOT IN (SELECT geocode FROM " + dbTableCaches + ")", null);

        Log.d("Database clean: removing non-existing caches from trackables");
        database.delete(dbTableTrackables, "geocode NOT IN (SELECT geocode FROM " + dbTableCaches + ")", null);

        Log.d("Database clean: removing non-existing caches from logcount");
        database.delete(dbTableLogCount, "geocode NOT IN (SELECT geocode FROM " + dbTableCaches + ")", null);

        DBLogOfflineUtils.cleanOrphanedRecords();

        Log.d("Database clean: removing non-existing caches from logs");
        database.delete(dbTableLogs, "geocode NOT IN (SELECT geocode FROM " + dbTableCaches + ")", null);

        Log.d("Database clean: removing non-existing logs from logimages");
        database.delete(dbTableLogImages, "log_id NOT IN (SELECT _id FROM " + dbTableLogs + ")", null);

        Log.d("Database clean: remove non-existing extension values");
        final DBExtensionType[] extensionValues = DBExtensionType.values();
        if (extensionValues.length > 0) {
            String type = "";
            for (DBExtensionType id : extensionValues) {
                type += (StringUtils.isNotBlank(type) ? "," : "") + id.id;
            }
            database.delete(dbTableExtension, "_type NOT IN (" + type + ")", null);
        }
        database.delete(dbTableExtension, "_type=" + DBEXTENSION_INVALID.id, null);
    }
Remove caches which are not on any list in the background. Once it has been executed once it will not do anything.
This must be called from the UI thread to ensure synchronization of an internal variable.
main/src/main/java/cgeo/geocaching/storage/DataStore.java