uninstall.php for WordPress Plugins

Every WordPress plugin you develop should always have an uninstall.php.  The purpose of this file is to erase all your plugin’s data when the user deletes it.  You don’t want to be the developer of a plugin that never deletes any of its data. So, start by adding an uninstall.php to your top directory and add this code to it.

<?php

/* if uninstall not called from WordPress exit */

if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
    exit ();

/* Delete all existence of this plugin */

global $wpdb;

/*
Drop Table if you created one

$table_name = 'your_table_name';

$wpdb->query('DROP TABLE `' . $table_name . '`');
*/

$blog_option_name = 'your_blog_option_name';
$site_option_name = 'your_site_option_name';
$post_meta_data_name = 'your_post_meta_data_name';
$user_meta_data_name = 'your_user_meta_data_name';

if ( !is_multisite() ) {

    /* Delete blog option */

    delete_option($blog_option_name);

    /* Delete post meta data */

    $posts = get_posts(array('posts_per_page' => -1));

    foreach ($posts as $post) {
        $post_meta = get_post_meta($post->ID);
        delete_post_meta($post->ID, $post_meta_data_name);
    }

    /* Delete user meta data */

    $users = get_users();

    foreach ($users as $user) {
        delete_user_meta($user->ID, $user_meta_data_name);
    }
}

else {

    /* Delete site option */

    delete_site_option($site_option_name);

    /* Used to delete each option from each blog */

    $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );

    foreach ( $blog_ids as $blog_id ) {

        switch_to_blog( $blog_id );

        /* Delete blog option */

        delete_option($blog_option_name);

        /* Delete post meta data */

        $posts = get_posts(array('posts_per_page' => -1));

        foreach ($posts as $post) {
            $post_meta = get_post_meta($post->ID);
            delete_post_meta($post->ID, $post_meta_data_name);
        }

        /* Delete user meta data */

        $users = get_users();

        foreach ($users as $user) {
            delete_user_meta($user->ID, $user_meta_data_name);
        }

    restore_current_blog();
    }
}
?>

Note that this is an example uninstall.php and you should change the code as needed.

The first question and concern you might have is making sure the plugin deletes all of its data for single and multi-sites.  Multi-sites is a new feature as of WordPress 3.0 and it makes it a little bit harder for developers to delete all plugin data upon its deletion.  For example, if your plugin saves data using the add_option() function to a single site, then all data is saved to that blog’s wp_options table.  To then delete that option, execute the delete_option() function in your uninstall.php.  To then delete that same blog option on a multi-site isn’t as easy.  The complicated thing about multi-sties is that a new wp_options table is created for each new blog.  So, your plugin then needs to delete each option from each blog’s wp_options table.  To do this we first need to obtain all blogs for this site.  Then loop through all blogs and delete each option from its respective wp_options table.  Finally, in a multi-site you can also save things to the entire site’s meta data using the add_site_option() function.  This is very simple to delete, just call the delete_site_option() in your uninstall.php.

There are other ways to store data in wordpress.  Your plugin can save meta data to a post with the add_post_meta() function, and to a user’s meta data with the add_user_meta() function.  The same process is used for deleting these options as it was for the blog option, but using the delete_post_meta() and delete_user_meta() functions instead.  The basic idea for a multi-site is to loop through all blogs and then delete all post and user meta data in the uninstall.php.  If this is a single site, then just delete each post and user meta data in the uninstall.php.

This post does not discuss how to properly create and drop custom wordpress tables for single or multi-sites. So, if you have created a new custom table in your plugin, please make sure to delete that as well. You can use this code as a reference.

Please contact us at feedback@wpdevadvice.com or support@wpdevadvice.com for recommendations and questions.  Also, check out our social media channels.

2 thoughts on “uninstall.php for WordPress Plugins”

Leave a Reply

Your email address will not be published. Required fields are marked *