Delete all WordPress subscribers with simple script

At some point you might need to delete all the WordPress Subscribers for some reason, mostly spam users. Here is a quick script to do it. Simply create a file in the root of you server with the following code:

define( 'WP_USE_THEMES', false );
 // Enter the correct path for the following 2 files if not within the current directory
 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 = "[[:<:]]{$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 );

Don’t forget to delete the file when done on the server so no one can launch this command again.

Source: http://wpforce.com/how-to-mass-delete-users-based-on-their-role/ (found issue in the snippet, just corrected them)

Leave Your Response

* Name, Email, Comment are Required