2

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?

  • You can run SQL queries directly on the database. Just make sure you back up your database before you start. – montrealist Jul 31 '13 at 14:52
  • Are there any other associated tables that I need to delete records from? – am71722 Jul 31 '13 at 14:58
  • Do you want to remove all of them? off course, except the administrator? – M-R Jul 31 '13 at 15:01
  • All except two users – am71722 Jul 31 '13 at 15:02
  • On a standard install they're just two tables: wp_users and wp_usermeta. – montrealist Jul 31 '13 at 15:18
1

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.

0

You can run SQL queries directly on the database. Just make sure you back up your database before you start. – dalbaeb

This link left by dalbaeb solved my problem: http://www.scriptol.com/wordpress/mass-delete-users.php

It deletes all the required records

Thanks all!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.