Skip to content

How To Bulk Delete WordPress Users Based On Their Role

If you want to delete users from your WordPress then you can’t remove more than 20 users at a time. You would have to go through every single page to delete every single one of them. We have a simple script which will allow you to bulk delete WordPress users based on their role. All you need to do is just run this script and that’s it.

Just put any of the following code to functions.php file of your current theme to bulk delete users of that role:

[alert style=”red”]Note: Please remove these codes immediately after saving your functions.php file. You only need to run these codes once, there’s no need to keep these codes in your theme.[/alert]

For Admin

function remove_administrators() {
    global $wpdb;
    $args = array( 'role' => 'Administrator' );
    $administrators = get_users( $args );
    if( !empty($administrators) ) {
        require_once( ABSPATH.'wp-admin/includes/user.php' );
        $i = 0;
        foreach( $administrators as $administrator ) {
            if( wp_delete_user( $administrator->ID ) ) {
                $i++;
            }
        }
        echo $i.' Administrators deleted';
    } else {
        echo 'No Administrators deleted';
    }
}
remove_administrators();

For Authors

function remove_authors() {
    global $wpdb;
    $args = array( 'role' => 'Author' );
    $authors = get_users( $args );
    if( !empty($authors) ) {
        require_once( ABSPATH.'wp-admin/includes/user.php' );
        $i = 0;
        foreach( $authors as $author ) {
            if( wp_delete_user( $author->ID ) ) {
                $i++;
            }
        }
        echo $i.' Authors deleted';
    } else {
        echo 'No Authors deleted';
    }
}
remove_authors();

For Editors

function remove_editors() {
    global $wpdb;
    $args = array( 'role' => 'Editor' );
    $editors = get_users( $args );
    if( !empty($editors) ) {
        require_once( ABSPATH.'wp-admin/includes/user.php' );
        $i = 0;
        foreach( $editors as $editor ) {
            if( wp_delete_user( $editor->ID ) ) {
                $i++;
            }
        }
        echo $i.' Editors deleted';
    } else {
        echo 'No Editors deleted';
    }
}
remove_editors();

For Contributors

function remove_contributors() {
    global $wpdb;
    $args = array( 'role' => 'Contributor' );
    $contributors = get_users( $args );
    if( !empty($contributors) ) {
        require_once( ABSPATH.'wp-admin/includes/user.php' );
        $i = 0;
        foreach( $contributors as $contributor ) {
            if( wp_delete_user( $contributor->ID ) ) {
                $i++;
            }
        }
        echo $i.' Contributors deleted';
    } else {
        echo 'No Contributors deleted';
    }
}
remove_contributors();

For Subscribers

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 ) ) {
                $i++;
            }
        }
        echo $i.' Subscribers deleted';
    } else {
        echo 'No Subscribers deleted';
    }
}
remove_subscribers();

4 thoughts on “How To Bulk Delete WordPress Users Based On Their Role”

  1. Hello Hardeep Asrani and congratulations for the post, very helpful.
    I wanted to ask if there is the possibility of eliminating eg all authors, automatically after x days, leaving the code, modified, fixed in the function.php file.

    Thank you!

      1. Thank you for the immediate response, and I’m sorry if I expressed myself badly.
        The WP-Cron executes functions perfectly, but what I want is to eliminate “every author after 365 days from the date of its registration.”
        example:

        A author enrolls the 2014/01/01
        The author is automatically deleted 2015/01/01

        A author enrolls the 01/06/2014
        The author is automatically deleted 01/06/2015

        A author enrolls the 03/01/2014
        The author is automatically deleted 03/01/2015

        etc. etc.

        Is it possible?

        Thank you very much!

Leave a Reply

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