Title: Query to delete Wp users
Last modified: August 22, 2016

---

# Query to delete Wp users

 *  [Euclides](https://wordpress.org/support/users/panafff/)
 * (@panafff)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/)
 * Hello guys, can someone help me with a spam issue? A friend´s site has been spammed,
   he uses buddypress and now the site about 20 K spam users. I would like to remove
   them all with a sql query, the website has mysql to handle the database. Do you
   have any command or script that help me do this quickly?
 * Thank you in advanced!

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [bernbe01](https://wordpress.org/support/users/bernbe01/)
 * (@bernbe01)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832079)
 * hey there, if you just want a query you could use something like this:
 * *substitute the user you want to keep for admin
 * ****this will delete all users not matched on execution! please backup your database
   first!
 * `DELETE FROM wp_users WHERE user_login <> 'admin'`
 * or if you have a list of users to keep:
 * `DELETE FROM wp_users WHERE user_login NOT IN ('admin','second_user_to_keep','
   etc')`
 * let us know if you need anything else
 *  Thread Starter [Euclides](https://wordpress.org/support/users/panafff/)
 * (@panafff)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832082)
 * Hi mernbe01, thank you for the query, I will try it and let you know how it goes.
 *  Thread Starter [Euclides](https://wordpress.org/support/users/panafff/)
 * (@panafff)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832087)
 * Hey dude, it worked perfectly! Thank you very much again. Have a good week!
 *  [bernbe01](https://wordpress.org/support/users/bernbe01/)
 * (@bernbe01)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832089)
 * you too! thanks for letting us know and feel free to mark the thread as resolved
   if you have time
 * take care
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832092)
 * One thing to rememember is that you should also delete anything in the `wp_usermeta`
   table as well, as there will be a _lot_ there if you’re trashing 20,000 users.
   🙂
 * That should be albe to be done by this (but take a backup of the table beforehand,
   just in case!!!):
 * `DELETE FROM wp_usermeta WHERE user_id NOT IN(1,2);`
 * The (1,2) are the ID’s of the users that you want to keep, comma separated.
 *  [bernbe01](https://wordpress.org/support/users/bernbe01/)
 * (@bernbe01)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832166)
 * darn i missed that! good catch and great point [@catacaustic](https://wordpress.org/support/users/catacaustic/)
 *  Thread Starter [Euclides](https://wordpress.org/support/users/panafff/)
 * (@panafff)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832171)
 * I just empty the table, thank you!
 *  Thread Starter [Euclides](https://wordpress.org/support/users/panafff/)
 * (@panafff)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832233)
 * Hey guys, my friends admin account is not working, actually the wp-admin link
   does not work now, it say:
 * You do not have sufficient permissions to access this page.
 * I tried to repair the database and tables but that din not work, any ideas?
 *  Thread Starter [Euclides](https://wordpress.org/support/users/panafff/)
 * (@panafff)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832234)
 * The problem seems to be after I ran the query to delete de wp_usermeta table
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832235)
 * > I just empty the table, thank you!
 * NOOOOOOO !!!!!!!!!!!!!!!
 * When you eptied the table, **you removed all of your own admin accounts details,
   settings and permissions**. That is why I had the `NOT IN()` clause in the SQL
   query… you do not want to delete anything from your own account.
 * Now, you did take a backup of that table before you did emptied it, didn’t you????
   If you did, re-install the values, and your account will be back and running.
   If you didn’t, you’ll have to talk to your hosting company to see if they have
   any backups that they can restore.
 *  [bernbe01](https://wordpress.org/support/users/bernbe01/)
 * (@bernbe01)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832237)
 * and if you dont want to use user id’s for your meta data delete, you can use 
   a query like this, just know it is slower to use subselects
 * make sure you edit the usernames in the subselect to the ones you want to keep
 * `DELETE FROM wp_usermeta WHERE user_id NOT IN(SELECT ID FROM wp_users WHERE user_login
   IN ('admin','second_user_to_keep','etc'))`

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Query to delete Wp users’ is closed to new replies.

## Tags

 * [users](https://wordpress.org/support/topic-tag/users/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 3 participants
 * Last reply from: [bernbe01](https://wordpress.org/support/users/bernbe01/)
 * Last activity: [11 years, 2 months ago](https://wordpress.org/support/topic/query-to-delete-wp-users/#post-5832237)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
