Lorraine1996
Lorraine1996

Reputation: 315

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?

Upvotes: 0

Views: 825

Answers (2)

Nathan Dawson
Nathan Dawson

Reputation: 19308

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/

Upvotes: 2

Suraj Sanwal
Suraj Sanwal

Reputation: 790

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

Upvotes: 1

Related Questions