Using WordPress ‘delete_plugins()’ PHP function

The delete_plugins() WordPress PHP function removes a plugin’s directory and files for a specified list of plugins.

Usage

Here’s a basic example of how to use the function:

delete_plugins( array( 'plugin-directory/plugin-file.php' ) );

In this example, ‘plugin-directory/plugin-file.php’ is the path to the plugin file you wish to delete, relative to the plugins directory.

Parameters

  • $plugins (string – Required): List of plugin paths to delete, relative to the plugins directory.
  • $deprecated (string – Optional): Not used. Default is an empty string ”.

More Information

See WordPress Developer Resources: delete_plugins()

This function was introduced in WordPress version 2.6.0. It’s not deprecated as of the last update to this guide. For the source code, you can visit the WordPress repository.

Examples

Delete a Single Plugin

This code will delete a single plugin. The plugin file path is ‘my-plugin/my-plugin.php’.

delete_plugins( array( 'my-plugin/my-plugin.php' ) );

Delete Multiple Plugins

To delete multiple plugins, simply add their paths to the array.

delete_plugins( array( 'my-plugin/my-plugin.php', 'another-plugin/another-plugin.php' ) );

Delete a Plugin With a Condition

You can add conditions before deleting a plugin. In this case, the plugin will only be deleted if it is not active.

if ( ! is_plugin_active( 'my-plugin/my-plugin.php' ) ) {
    delete_plugins( array( 'my-plugin/my-plugin.php' ) );
}

Delete a Plugin and Redirect

After deleting a plugin, you might want to redirect the user to a specific page. Here’s how:

delete_plugins( array( 'my-plugin/my-plugin.php' ) );
wp_redirect( admin_url( 'plugins.php' ) );
exit;

Delete a Plugin and Show a Message

This example deletes a plugin and then displays a message.

if ( delete_plugins( array( 'my-plugin/my-plugin.php' ) ) ) {
    echo 'The plugin has been successfully deleted.';
} else {
    echo 'There was an error deleting the plugin.';
}