-WP Multisite- How to also drop plugin database tables when blog is deleted?

Is there a way to also drop plugin database tables when blog is deleted? As i observed, only the default wordpress database tables are dropped but not the plugin generated tables.

Please help.

Related posts

Leave a Reply

1 comment

  1. I think you’ll have to code up a custom plugin for this. During the Delete Site process, there are many actions and filters that you can hook into. One in particular you should look at is:

    wp-admin/includes/ms.php Line 86:
    $drop_tables = apply_filters( 'wpmu_drop_tables', $wpdb->tables( 'blog' ) );
    

    So in your custom plugin, address the troublesome plugin tables directly. Add something like,

    function remove_plugin_xyz_tables($tables) {
       global $wpdb;
       $id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
       $tables[] = $wpdb->get_blog_prefix($id).'table_xyz';
       return $tables;
    }
    add_filter('wpmu_drop_tables', 'remove_plugin_xyz_tables');
    

    Some caveats: I don’t know how WordPress will handle the situation if the table doesn’t exist, so you’d want to test that and you may want to first check to see if it exists and/or if the plugin is active and/or check to see if it’s in the array already. This also requires you to add a line for each troublesome plugin, and it assumes the plugin has properly prefixed its tables. An alternative would be to 'SHOW TABLES LIKE "'.$wpdb->get_blog_prefix($id).'%" and merge your result with what WP already knows about. Lastly, I didn’t test any of this, so this code is merely to point you in the right direction on developing a solid solution for yourself.

    Anyway, once your plugin is done, network activate it, and it should take care of all the dirty work for you when a site is deleted!

    Oh yeah, and alert the plugin author to the issue to. It’s possible that s/he doesn’t use WPMS and isn’t even aware that this is an issue. If you have the means to do so, code up a patch and submit it to that person to save them the work. Open source FTW.

    Best of luck~