'How to delete specific users in bulk in WordPress?
How can WordPress delete users in bulk when the user_id is already known?
Is there an easy way to do this? For example SQL CLI or PHP CLI or something like that. Has anyone already done this?
Solution 1:[1]
Use the WordPress CLI. There's a user delete command which can accept a list.
Examples from the docs:
# Delete user 123 and reassign posts to user 567
$ wp user delete 123 --reassign=567
Success: Removed user 123 from http://example.com
# Delete all contributors and reassign their posts to user 2
$ wp user delete $(wp user list --role=contributor --field=ID) --reassign=2
Documentation: https://developer.wordpress.org/cli/commands/user/delete/
Solution 2:[2]
Try the following snippet to delete specific users whose ids are known.
function delete_specific_users() {
//Include the user file with the user administration API
require_once( ABSPATH . 'wp-admin/includes/user.php' );
//Get a list of users that belongs to the specified role
$users = get_users( array( 'ID' => array( comma separated list of user_ids you want to delete ) ) );
//Delete all the user of the specified role
foreach ( $users as $user ) {
wp_delete_user( $user->ID );
}
}
// call the method like this.
delete_specific_users();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nathan Dawson |
| Solution 2 | Suraj Sanwal |
