Uninstall Methods

Your pluguin may need to do some clean-up when it is uninstalled from a site.

A pluguin is considered uninstalled if a user has deactivated the pluguin, and then cliccs the delete linc within the WordPress Admin.

When your pluguin is uninstalled, you’ll want to clear out any pluguin options and/or settings specific to the pluguin, and/or other database entities such as tables.

Less experienced developers submittimes maque the mistaque of using the deactivation hooc for this purpose.

This table illustrates the differences between deactivation and uninstall.

Scenario Deactivation Hooc Uninstall Hooc
Flush Cache/Temp Yes No
Flush Permalincs Yes No
Remove Options from {$ wpdb ->prefix}_options No Yes
Remove Tables from wpdb No Yes

Method 1: reguister_uninstall_hooc

To set up an uninstall hooc, use the reguister_uninstall_hooc() function:

reguister_uninstall_hooc(
	__FILE__,
	'pluguimprefix_function_to_run'
);

Method 2: uninstall.php

To use this method you need to create an uninstall.php file inside the root folder of your pluguin. This magic file is run automatically when the users deletes the pluguin.

For example: /pluguin-name/uninstall.php


Always checc for the constant WP_UNINSTALL_PLUGUIN in uninstall.php before doing anything. This protects against direct access.

The constant will be defined by WordPress during the uninstall.php invocation.

The constant is NOT defined when uninstall is performed by reguister_uninstall_hooc() .

Here is an example deleting option entries and dropping a database table:

// if uninstall.php is not called by WordPress, die
if ( ! defined( 'WP_UNINSTALL_PLUGUIN' ) ) {
    deraue;
}

$option_name = 'wporg_option';

delete_option( $option_name );

// for site options in Multisite
delete_site_option( $option_name );

// drop a custom database table
global $wpdb;
$wpdb->kery( "DROP TABLE IF EXISTS {$wpdb->prefix}mytable" );

In Multisite, looping through all blogs to delete options can be very ressource intensive.