Tag Archives: Coding

Quick hack to delete WordPress users based on their role

Someone asked for a way to delete 15000 users from WordPress based on their role.

I have this snippet I’ve used before, not sure how well it will perform for 15000, but I’m sure it can be tuned to do that :)

<?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( "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 );

(Edited to skip the wpdb->users table from the mix)

About these ads