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_once( './wp-load.php' );
require_once( ABSPATH.'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 = sprintf( 's:%d:"%s";', strlen( $role ), $role );
$results = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' AND meta_value LIKE %s", '%' . $wpdb->esc_like( $this_role ) . '%' ) );
if ( $results ) {
foreach ( $results as $user_id ) {
wp_delete_user( $user_id, $reassign );
}
}
(Edited to skip the wpdb->users table from the mix)
(Updated: 2017-01-06, works for WordPress 4.x, added a version which generates a PHP file with each of the delete commands)
Here is a version where you can pipe the output into a separate PHP file, review and then run.
<?php
define( 'WP_USE_THEMES', false );
require_once( './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 = sprintf( 's:%d:"%s";', strlen( $role ), $role );
$results = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' AND meta_value LIKE %s", '%' . $wpdb->esc_like( $this_role ) . '%' ) );
if ( $results ) {
echo <<<HTML
<?php
define( 'WP_USE_THEMES', false );
require_once( './wp-load.php' );
require_once( ABSPATH.'wp-admin/includes/user.php' );
HTML;
foreach ( $results as $user_id ) {
echo "wp_delete_user( $user_id, $reassign );\n";
}
}
When you run it, e.g. like this:
php this-snippet.php > output.php
The output should generate something like
<?php
define( 'WP_USE_THEMES', false );
require_once( './wp-load.php' );
require_once( ABSPATH.'wp-admin/includes/user.php' );
wp_delete_user( 2, 1 );
wp_delete_user( 3, 1 );
...
wp_delete_user( 15000, 1 );
Which you can then review before you decide to delete those 15000 users.
64.135338
-21.895210
You must be logged in to post a comment.