WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to be able to automatically uninstall certain plugins if they're detected (specifically Akismet and Hello Dolly), either by writing another plugin to do so or via my theme's functions.php file. Is that possible?

share|improve this question
up vote 1 down vote accepted

Sure, just call the delete_plugins() function, found in wp-admin/includes/plugin.php - you have to manually require() it.

share|improve this answer

This'll deactivate and uninstall plugins you specify in the the $to_deactivate and $to_delete arrays.

NOTE: Due to the funky way WordPress activates plugins, we can't use an activation hook to do our work. Instead we check for the existence of an option, so as to ensure it only runs once per activation!


 * Plugin Name: My Remove Plugins
 * Version: 0.1

 * Deactivate and/or delete plugins.
function my_remove_plugins()
    $to_deactivate = array( 'Anti-Spam Email' );
    $to_delete     = array( 'Akismet' );

    $deactivate_stack =
    $delete_stack     = array();

    foreach ( get_plugins() as $plugin => $data ) {
        if ( in_array( $data['Name'], $to_deactivate ) )
            $deactivate_stack[] = WP_PLUGIN_DIR . "/$plugin";

        elseif ( in_array( $data['Name'], $to_delete ) )
            $delete_stack[] = WP_PLUGIN_DIR . "/$plugin";

    if ( !empty($deactivate_stack) || !empty($delete_stack) )
        deactivate_plugins( array_merge( $deactivate_stack, $delete_stack ), true );

    if ( !empty($delete_stack) )
        $result = delete_plugins( array_map( 'plugin_basename', $delete_stack ) );

    update_option( 'my_remove_plugins', 1 );

    // refresh, otherwise deleted plugins will still show on first load
    wp_safe_redirect( $_SERVER['REQUEST_URI'] );

if ( is_admin() && !get_option( 'my_remove_plugins' ) )
    add_action( 'admin_init', 'my_remove_plugins' );

 * Clear out our option on deactivation.
function my_remove_plugins_deactivate()
    delete_option( 'my_remove_plugins' );
register_deactivation_hook( __FILE__, 'my_remove_plugins_deactivate')
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.