How does uninstalling WordPress plugins work?

I’m trying to drop a database table when the user uninstalls the plugin. But what does uninstalling a WordPress plugin really mean?

There’s the deactivation hook and there’s also the uninstall hook. And then there’s the uninstall.php file.

Read More

On the constructor of the plugin class I have the uninstall hook:

register_uninstall_hook(__FILE__, array($this, 'uninstall_housekeeping'));

Then the uninstall_housekeeping method has the following code:

public function uninstall_housekeeping(){

global $wpdb;
$tweets_table = $wpdb->prefix . 'zam_tweets';

require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta("DROP TABLE $tweets_table");
}

According to the wordpress codex you need to have an uninstall.php file which contains the code that will execute when the plugin is uninstalled so I also put this code:

if (!defined('WP_UNINSTALL_PLUGIN'))
exit();
global $wpdb;
$tweets_table = $wpdb->prefix . 'zam_tweets';

require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta("DROP TABLE $tweets_table");

But all I can see on the WordPress plugin page is the activate and delete links when the plugin is currently deactivated. And then the deactivate and edit link if its currently active. Where’s uninstall? I already tried the delete link but it doesn’t seem like its doing anything except to delete the whole plugin folder, the database table is still intact.

Related posts

1 comment

  1. As explained in my comment in the Question, the error is certainly in how the DROP TABLE is being performed. But answering to How does uninstalling WordPress plugins work?:

    If we do a register_uninstall_hook in our plugin, the callback is stored in the option uninstall_plugins in /wp-includes/plugin.php.

    $uninstallable_plugins[plugin_basename($file)] = $callback;
    update_option('uninstall_plugins', $uninstallable_plugins);
    

    In the PHPDoc for the function register_uninstall_hook we have this:

    This file (uninstall.php) will be called, if it exists, during the uninstall process bypassing the uninstall hook.

    And the bypass happens in /wp-admin/includes/plugin.php:

    define('WP_UNINSTALL_PLUGIN', $file);
    include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
    return true;
    

    So, whatever you have in the uninstall hook or file, it should work by its own. If it does, it will work when the uninstall happens.

Comments are closed.