Delete all users with specific role by running the code

Allowing everyone to register to your site isn’t always a good idea. But sometimes you will want that option and you will want people to leave their trail on the site, to comment on your articles more easily, even write articles or become members in order to get discounts, etc.

While you can take precaution measures and control email domains which can be registered and even have several security plugins (like Security Ninja and Login Ninja which we suggest having on your every WordPress site) installed, you will never be 100% secure from random sign-ups and possible hacker attacks.

Even if you take complete control over registrations, you can lose that control in a blink of an eye.

In that case, you might end up in hundreds if not thousands of registered spam bots ready to take over your comments section. And since your first line of defense failed, you will have to delete those users from your database.

If there are only dozens of those users, it won’t be hard to manually go through users list and select users who you want to delete. More often than you think, there will be thousands of users to delete and manual deletion won’t be a choice. In this case, use the code we’re about to show you which will run through your user list and delete all users with specified role:

Delete all users with a specific role:

  1. Open functions.php file
  2. Copy and paste the following function:
  3. function remove_subscribers() {
    global $wpdb;
    $args = array( 'role' => 'Subscriber' );
    $subscribers = get_users( $args );
    if( !empty($subscribers) ) {
    require_once( ABSPATH.'wp-admin/includes/user.php' );
    $i = 0;
    foreach( $subscribers as $subscriber ) {
    if( wp_delete_user( $subscriber->ID ) ) {
    echo $i.' Subscribers deleted';
    } else {
    echo 'No Subscribers deleted';
  4. Save changes

After you’ve made those changes to functions.php file, it will be enough to reload the page so that the function can do its magic.

Depending on user count, the function might take several minutes to do its work so don’t panic and wait for the process to be completed.

Once the function is over with listing and deleting users, it is important that you go back to functions.php file and delete it. If you leave the function, it will keep deleting your subscribers every time you reload any page.

While this function will delete subscribers, you can easily modify it to delete any other role. You don’t even have to change all variable names, but simply change user role on the 3rd line of the code:

$args = array( 'role' => 'Contributor' );

Define the number of users to be deleted:

In some cases (when there are thousands of users you want to delete) it might be a good idea to make it easy on your database and your server. Instead of deleting each and every one of them at once, which may have a big impact on your database, you could ease up the code and limit the number of users which will be deleted each time the script is run. In that case, you should modify the code and add the break when the counter comes to a certain number:

foreach( $subscribers as $subscriber ) {
if($i==500) break;
if( wp_delete_user( $subscriber->ID ) ) {

In this case, after you save changes to functions.php, make sure to run the code several times in order to delete all users. Each time that you reload, a function will delete only up to 500 users from the database (or any number that you entered as breaking number).

Once again, don’t forget to remove the code after your job is done.

Start Blog Book


This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *