'Cleans up all SearchCommunity and MetadataCommunity stuff in dispersy database.'

def _purge_old_search_metadata_communities(self):

    db_path = os.path.join(self.session.config.get_state_dir(), u'sqlite', u'dispersy.db')
    if (not os.path.isfile(db_path)):
        return
    communities_to_delete = (u'SearchCommunity', u'MetadataCommunity', u'TunnelCommunity')
    connection = Connection(db_path)
    cursor = connection.cursor()
    for community in communities_to_delete:
        try:
            result = list(cursor.execute(u'SELECT    id    FROM    community    WHERE    classification    ==    ?;', (community,)))
            for (community_id,) in result:
                cursor.execute(u'DELETE    FROM    community    WHERE    id    ==    ?;', (community_id,))
                cursor.execute(u'DELETE    FROM    meta_message    WHERE    community    ==    ?;', (community_id,))
                cursor.execute(u'DELETE    FROM    sync    WHERE    community    ==    ?;', (community_id,))
        except StopIteration:
            continue
    cursor.close()
    connection.commit()
    connection.close()
