WordPress Plugin Deinstall Data Automatically

In version 2.7 of WordPress you will have the possibility to delete a Plugin directly in your backend, see version 2.7 info. To delete also the Plugin data from your database, the Plugin author has to accomplish some requirements. I hope the following tutorial will help some Plugin authors and they will implement this feature in their current or future Plugins.

since version 2.7 you can delete Plugins directly in your WordPress backend

Everybody who had the uninstall function in their Plugin, doesn’t have to change a lot. For everybody who likes to know, you can find the function for uninstall in wp-admin/includes/plugin.php

  1. is_uninstallable_plugin($plugin)
  2. uninstall_plugin($plugin)

and the associated hook wp-includes/plugin.php

  1. register_uninstall_hook($file, $callback)

You can use two possibilities to add the additional function.

  1. A file with the name uninstall.php has to be in the Plugin.
  2. A hook for uninstall register_uninstall_hook has to talk to.

Up to Version 2.7 you were also able to delete the data from the database, but only if you deactivated the Plugin (Hook register_deactivation_hook()) or with help of a formular.

To use also other versions of WordPress, I suggest to use the constant WP_UNINSTALL_PLUGIN, because this one exists in version 2.7. Alternative you can use the hook register_uninstall_hook(), since this one is necessary to function via hook. Both possibles are listed in the following examples.

in version 2.7 you can delete Plugins directly in your backend

The following function for example should be in the Plugin and deleting the entry example from the database table options.

/**
 * Delete options in database
 */
function example_deinstall() {

	delete_option('example');
}

via Data uninstall.php

Easiest possibility is to create the file unistall.php with according entries to delete data in the table options.

delete_option('example');

To avoid an error in older WordPress versions, I recommend to use a constant. Isn’t there a constant defined, it will just stop in this example.

if ( !defined('WP_UNINSTALL_PLUGIN') ) {
    exit();
}
delete_option('example');

via hook register_uninstall_hook()

Alternatively you can register the deinstallation via hook, so that you are not depend on the file and you can use a easier possibility to integrate an uninstallation in your Plugin. For that you can use the hook register_uninstall_hook().

 /**
 * Check for hook
 */
if ( function_exists('register_uninstall_hook') )
	register_uninstall_hook(__FILE__, 'example_deinstall');

 /**
 * Delete options in database
 */
function example_deinstall() {

	delete_option('example');
}

The possibility, to delete Plugins directly in your backend, certainly makes it easier for the user to get rid off unused data. But for that, the Plugin authors have to support this feature and the table options certainly will be thankful for that.

Comments are closed.

3 comments

  1. Ade

    Frank,

    Thanks for the very useful article. A couple of questions, if I may:

    1. In your opinion which of the two methods (file or hook) is better?

    2. To maintain backwards compatibility with pre 2.7 versions of WP, it seems to me that it would be useful to keep the register_deactivation_hook with a function to delete options, called by a conditional check if WP_UNINSTALL_PLUGIN doesn't exist. Does this seem a good idea to you? If so, it seems to me that using the uninstall hook would be the best option (rather than use an uninstall file).

    I'm fairly new to all this, so any pointers you can give me will be much appreciated!

    Thanks in advance. Once again, thanks for the useful articles on your site.

    Ade.

  2. Frank

    @Ade:

    1. I think Hook ist better; no new request.
    2. yes, i see also this

    My favorite way ist the hook, small, easy and clean.
    Best wishes.

  3. Juergen

    I have to correct myself
    It's delete_metadata
    with
    delete_metadata('post', null, 'my_metadata_key', null, $delete_all = true);

    you can delete all post metadate with the key 'metadata'