How to Mass Delete Users Based on their Role

@MattBrett tweeted out a couple days ago, asking how to mass delete users specific to what role they were. For example, being able to delete 15,000 users that were all “subscribers” in WordPress.

There is not a native way to do this, as this would be a rare edge case that you would either have that many users in a single installation of WordPress, or have the need to delete that many users if you had that number of users to begin with. Either way, it’s not very common to do in WordPress.

Egill saw the tweet and decided to whip up a little code to get the job done.

Put this into any file in the WordPress root and it will work, for example, bulkdelete.php … and then access the file on the web.

<?php
define( 'WP_USE_THEMES', false );
require( './wp-load.php' );

$role = "subscriber"; // The role to kill
$reassign = 1; // The user that all posts will fall back to, other wise they will be deleted

$this_role = "[[:<:]]$role[[:>:]]";

$query = $wpdb->prepare( "SELEC T user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' AND meta_value RLIKE %s", $this_role );

if ( $users_of_this_role = $wpdb->get_results( $query, ARRAY_N ) )
	foreach ( $users_of_this_role as $user_id )
			wp_delete_user( $user_id[0], $reassign );

He works for Automattic doing lots of data related things, so it’s a given he knows what he’s doing. Here’s a little excerpt about Egill,

Although born and bred in Iceland, he whose name can not be spoken, bares no responsibility for ash spewing, holiday wrecking, flight grounding volcanoes with unpronounceable names. At Automattic he serves up a refreshing cocktail of data, stats and ads. While not working he can be found enjoying life with his better half, watching his three upcoming Automatticians growing up.

via Automattic.com

Comments

  1. By Dave on

    This doesn’t work. I get this error: Parse error: syntax error, unexpected ‘[‘, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/username/public_html/bulkdelete.php on line 8

    Reply »

    • By Jonathan Dingman on

      Hmm I see your point. I had an issue as well. I’ll talk to the snippet author and see if we can get a fix for it.

      Reply »

  2. By on

    I tried this, but it didn’t work.
    You can use the following, with a query var to select the user role instead …

    add_action( ‘init’, ‘mn_delete_users_by_role’ );
    function mn_delete_users_by_role() {
    if ( empty( $_GET[ ‘delete_role’ ] ) )
    return;

    // Needed to make use of wp_delete_user()
    require_once( ABSPATH . ‘/wp-admin/includes/user.php’ );

    // list of users with no posts and no comments
    $bla = new WP_User_Query( array( ‘role’ => $_GET[ ‘delete_role’ ] ) );

    $userList = $bla->get_results();
    foreach ($userList as $u) {
    echo $u->ID . ‘ ‘;
    wp_delete_user( $u->ID );
    }
    die( ‘done’ );

    }

    Reply »

  3. By Guillaume on

    Many errors in the snippet, here is how it should work (make sure the includes have the right path):

    define( ‘WP_USE_THEMES’, false );
    include( ‘wp-load.php’ );
    include( ‘wp-admin/includes/user.php’ );

    $role = “subscriber”; // The role to kill
    $reassign = 1; // The user that all posts will fall back to, other wise they will be deleted

    $this_role = “[[::]]”;

    $query = $wpdb->prepare( “SELECT user_id FROM $wpdb->usermeta WHERE meta_key = ‘{$wpdb->prefix}capabilities’ AND meta_value RLIKE %s”, $this_role );

    if ( $users_of_this_role = $wpdb->get_results( $query, ARRAY_N ) )
    foreach ( $users_of_this_role as $user_id )
    wp_delete_user( $user_id[0], $reassign );

    Reply »

Leave a Reply

Your email address will not be published. Required fields are marked *