How to Drop Tables on Uninstall using WordPress?

mysql, uninstallation, wordpress

Solution

You should not make uninstall.php as bhuthecoder said. Using an uninstall.php file to handle plugin uninstallation can be problematic because it relies on the plugin being properly installed and activated before it can be run. If the plugin was never activated, or if it was deactivated and then uninstalled, the uninstall.php file will not be run and any cleanup tasks that it was responsible for will not be performed. This can lead to plugin-specific data being left behind in the database, which can cause problems if the plugin is re-installed at a later time. A better approach is to use a WordPress uninstall hook.

You can name the file like you want or you can do that in single file without separate file for uninstallation.

register_uninstall_hook('uninstall.php', 'on_uninstall');

It means that WP will run uninstall.php and function on_uninstall in the uninstall.php when the plugin will be deleted. So, you can rename the file if you want, you can rename the function.

register_uninstall_hook(__FILE__, 'on_uninstall');

It means the same, but `__FILE__` indicate current file which is working now and function on_uninstall should be in this file.

FILE php.net

The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.

Simple example of functions for activation/deactivation/uninstallation.

function on_activation()
{
    //Some stuff
}

function on_deactivation()
{
    //Some stuff
}

function on_uninstall()
{
    //Some stuff
}

register_activation_hook(__FILE__, 'on_activation');
register_deactivation_hook(__FILE__, 'on_deactivation');
register_uninstall_hook(__FILE__, 'on_uninstall');

If you added some options you can delete it when uninstall like this:

delete_option( 'some name' );

As bhuthecoder said, you should not drop tables when deactivation, it is redundant and not friendly for a user, who can lost his data after deactivation.

There are syntax mistake in your code:

$sql = 'DROP TABLE IF EXISTS $table_name;';

Should be like this:

$sql = "DROP TABLE IF EXISTS $table_name";

If you want to put some variable in the string you should use double quotes.

And require_once is redundant. Better like this:

function e34s_db_clients_uninstall()
{
    global $wpdb;
    $table_name = $wpdb->prefix . 'e34s_clients';
    $sql = "DROP TABLE IF EXISTS $table_name";
    $wpdb->query($sql);
    delete_option('e34s_time_card_version');
}

register_uninstall_hook(__FILE__, 'e34s_db_clients_uninstall');

Or like this:

register_uninstall_hook('uninstall.php', 'e34s_db_clients_uninstall');

With function e34s_db_clients_uninstall in the uninstall.php.

Problem

On plugin activation, I have 4 tables that are created in the WordPress database. On uninstall I'd like to have the tables deleted. I've been able to write it out to delete the tables on deactivation. But from what I've read, it's better to keep the database tables information until the admin uninstalls rather than deactivates. I've been looking for answers but all I seem to able to find is dropping them on deactivation. I do also have the version option I need to uninstall with it.

Original source