Using WordPress ‘pre_uninstall_plugin’ PHP action

The pre_uninstall_plugin WordPress PHP action fires immediately before a plugin is uninstalled.

Usage

add_action('pre_uninstall_plugin', 'my_custom_pre_uninstall_plugin', 10, 2);

function my_custom_pre_uninstall_plugin($plugin, $uninstallable_plugins) {
    // your custom code here
}

Parameters

  • $plugin (string): Path to the plugin file relative to the plugins directory.
  • $uninstallable_plugins (array): An array of uninstallable plugins.

More information

See WordPress Developer Resources: pre_uninstall_plugin

Examples

Log plugin uninstallation

Log the name of the plugin being uninstalled in a custom log file.

add_action('pre_uninstall_plugin', 'log_plugin_uninstall', 10, 2);

function log_plugin_uninstall($plugin, $uninstallable_plugins) {
    $log_message = "Uninstalling plugin: " . $plugin . "\n";
    error_log($log_message, 3, "/path/to/your/logfile.log");
}

Prevent specific plugin uninstallation

Prevent the uninstallation of a specific plugin by checking its name.

add_action('pre_uninstall_plugin', 'prevent_plugin_uninstall', 10, 2);

function prevent_plugin_uninstall($plugin, $uninstallable_plugins) {
    if ($plugin == 'my-important-plugin/my-important-plugin.php') {
        wp_die("You cannot uninstall this important plugin.");
    }
}

Delete custom plugin options

Delete custom options created by a plugin during its uninstallation process.

add_action('pre_uninstall_plugin', 'delete_custom_plugin_options', 10, 2);

function delete_custom_plugin_options($plugin, $uninstallable_plugins) {
    if ($plugin == 'my-custom-plugin/my-custom-plugin.php') {
        delete_option('my_custom_plugin_option');
    }
}

Send email notification on plugin uninstallation

Send an email to the administrator when a specific plugin is uninstalled.

add_action('pre_uninstall_plugin', 'send_email_on_plugin_uninstall', 10, 2);

function send_email_on_plugin_uninstall($plugin, $uninstallable_plugins) {
    if ($plugin == 'my-notifiable-plugin/my-notifiable-plugin.php') {
        $to = get_option('admin_email');
        $subject = 'Plugin Uninstalled';
        $message = 'The My Notifiable Plugin has been uninstalled.';
        wp_mail($to, $subject, $message);
    }
}

Deactivate dependent plugins

Deactivate other plugins that depend on a specific plugin before it’s uninstalled.

add_action('pre_uninstall_plugin', 'deactivate_dependent_plugins', 10, 2);

function deactivate_dependent_plugins($plugin, $uninstallable_plugins) {
    if ($plugin == 'my-main-plugin/my-main-plugin.php') {
        deactivate_plugins('dependent-plugin/dependent-plugin.php');
    }
}