Is it possible to run plugin code when a multisite blog is deleted?

I have a plugin that creates tables when activated or when a new blog is added to a network. When a blog is deleted I would like to be able to remove those tables for that blog. Is there a hook available to do this?

I imagine it would be something similar to wpmu_new_blog, but I can’t find an equivalent for deleting.

Related posts

Leave a Reply

2 comments

  1. The function wpmu_delete_blog in /wp-admin/includes/ms.php has an action hook called delete_blog. This hook passes the variable of $blog_id

    You could try plugging into that hook, although it is executed right at the beginning of the function.

  2. This is arguably the better way to do it if you’re just deleting tables, but nothingtosee’s answer is also valid, and would be the required way if you need to do something besides dropping tables.

    add_filter("wpmu_drop_tables", "DropTablesForBlog");
    
    
    function DropTablesForBlog($tables)
    {
      global $wpdb;
    
      $tables[] = $wpdb->prefix . "my_plugin_table_1";
      $tables[] = $wpdb->prefix . "my_plugin_table_2";
      $tables[] = $wpdb->prefix . "my_plugin_table_2";
    
      return $tables;
    }