Bulk Delete Users Error uri too large

I am trying to delete thousands of users at once. After seeing that I can only display up to 999 user records at once using the “Screen Options” menu I thought that might be ok. But upon clicking delete I get a “Request-URI Too Large” error.

How can I delete these users without doing it 10 or 20 at a time?

2 Answers
2

I would shy away from trying to do this is SQL. It is possible but probably not necessary and is more prone to error that using WordPress Core functions.

You could use get_users() or WP_User_Query to retrieve your users but given that you only want to keep two users and you only need the ID for wp_delete_user those are pretty heavy. I’d just run a quick query on the user table and loop through it.

$users = $wpdb->get_col("SELECT ID FROM {$wpdb->users} WHERE ID NOT IN (1,2)");
// var_dump($users); // debug
foreach ($users as $u) {
  wp_delete_user($u);
}

That is irreversible.

Obviously you will have to supply the two user IDs for the users you want to keep.

Test on trash data and make sure that is what you really want to do. Please also note that unless a second parameter is passed wp_delete_user will delete posts associated with the deleted user.

If the $reassign parameter is not assigned to a User ID, then all
posts will be deleted of that user. The action ‘delete_user’ that is
passed the User ID being deleted will be run after the posts are
either reassigned or deleted. The user meta will also be deleted that
are for that User ID.

Leave a Comment