Home > other >  How to delete specific users in bulk in WordPress?
How to delete specific users in bulk in WordPress?

Time:04-11

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?

CodePudding user response:

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/

CodePudding user response:

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