Uninstall Methods

Standard
[display-posts post_type="content" id="3672" include_content=true include_title=false wrapper="div" wrapper_class=""]

Uninstall Methods

Your plugin may need to do some clean-up when it is uninstalled from a site. A plugin is considered uninstalled if a user has deactivated the plugin, and then clicks the delete link.

When your plugin is uninstalled, you’ll want to clear out any rewrite rules added by the plugin, options and/or settings specific to to the plugin, or other database values that need to be removed. Less experienced developers sometimes make the mistake of using the deactivation hook for this purpose.

This table illustrates the differences between deactivation and uninstall.

Scenario Deactivation Hook Uninstall Hook
Remove option from wp_options  —  X
Flush Cache  X  —

Instead of using the deactivation hook, you should do one of the following:

Here is an example of using uninstall.php.

uninstall.php #

When using uninstall.php, the plugin should always check for the WP_UNINSTALL_PLUGIN constant, before executing. The WP_UNINSTALL_PLUGIN constant is defined by WordPress at runtime during a plugin uninstall, it will not be present if uninstall.php is requested directly. It will also not be present when using the uninstall hook technique. WP_UNINSTALL_PLUGIN is only defined when an uninstall.php file is found in the plugin folder.

Here is an example removing database entries.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// If uninstall is not called from WordPress, exit
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit();
}
 
$option_name = 'plugin_option_name';
 
delete_option( $option_name );
 
// For site options in Multisite
delete_site_option( $option_name ); 
 
// Drop a custom db table
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}mytable" );

Note: In Multisite, looping through all blogs to delete options can be very resource intensive.

 
Skip to toolbar