Check out the "Digging into wordpress book by: "by Chris Coyier and Jeff Starr."
How to Get Your WordPress Plugin To DROP TABLE From The Database
Posted on January 28, 2010 by Marty
I’m going to show you an easy way, how to get rid of your database tables & options when the plugin is deactivated, that’s if you want them deleted?
If you’ve ever made a plugin or thought about making a plugin for wordpress which includes its own set of database tables, then the chances are (like most plug-ins) they wont remove the data when the plug-ins been de-activated.
In most cases this is to preserve the data in those tables, Just in case the user wants to reactivate the plugin with all the options still saved, or in other case’s, it’s just plain lazy?
The Function
<?php
function pluginUninstall() {
global $wpdb;
$thetable = $wpdb->prefix."your_table_name";
//Delete any options that's stored also?
//delete_option('wp_yourplugin_version');
$wpdb->query("DROP TABLE IF EXISTS $thetable");
}
?>
What you have to do is hook this Function into wordpress.
So when the plugin is de-activated you want this code to run…
The Hook
To do this, we call…
register_deactivation_hook($file, $function)
This wordpress function takes 2 parameters in order for it to work…
- The File to be called
- The Function to run
Example:
If your plugin is located in either of following locations
http://www.yourblog.com/wp-content/plugins/yourplugin.php or
http://www.yourblog.com/wp-content/plugins/yourplugin/plugin-name.php
then you can use this..
register_deactivation_hook( __FILE__, ‘pluginUninstall’ );
The __FILE__ will point to were your function is located, (The Current File your working in) and the 2nd part after the comma is the name of the function which is being passed, in this case were calling our pluginUninstall function, this will then remove all the options and tables that’s been added in the function.
Preserve Your Data
If your plugin needs to/or you want it to, keep some form of data behind after its de-activated, then you can simply write another function to handle that request, maybe you have an admin options page for your plugin? on there you can add a small section with a form, this can just have a button, which when pressed it can run the function and remove whatever data you would like…
You know of any other way? feel free to share!
Comments (3)











yeah, this is a good solution. I hope all plugin developer also follow this approach, especially with all their plugin information in option table
cause they just become a mess or unneeded information if we have unplugged and delete the plugin.
Hi there. Just wondering – wouldn’t it be better to register this functionality with “register_uninstall_hook” hook instead? The admin may want to deactivate the plugin only temporarily (it’s interferring with other plugins or causing some unwanted behavior which the admin must debug).
I believe the currently suggested way of doing this is by creating an uninstall.php file in the root of your plugin folder.
From there it’s really easy to remove options and database tables that have been created by your plugin. Jacob Santos explains the process here: http://jacobsantos.com/2008/general/wordpress-27-plugin-uninstall-methods/
I think it’s a good idea to remove the database tables and options on Uninstall rather then on deactivation. This allows the data to persist say if, as @MostlyCarbon mentioned, the admin wishes to debug his site and not lose any data.