Bulk delete users by role? SQL?

I need to bulk-delete a large number (4,321) of users, all of a particular role type (not an out-of-the-box role name).

I have tried several functions found online, and have tried the Bulk Delete (WPBulk) plugin, but both throw a 500 server error.
(I recently extended by server’s max value to allow for more processing).
I suspect changing the number of users visible on the Users listing might break things.

I suspect the best answer is to use SQL inside phpMyAdmin. I would need some hand-holding (ie. enter a query in to the “SQL” box in the UI?).
The users have many custom fields inside wp_usermeta.


Solutions Collecting From Web of "Bulk delete users by role? SQL?"

This can be done efficiently with a SQL query, but it won’t be so easy to set it up. It would have to delete the users, its meta data, to reassign their associated posts, etcetera.

As it is an operation that will run only once and will not have any long-lasting performance impact, I think it can be achieved using some WordPress core functions in a very easy way:


        Plugin Name: Delete users by role
        Description: Delete all the users with a hardcoded specific role
        Version: 0.1
        Author: Your name
        Author URI: http://www.yoursite.com/

    function wpse220426_delete_users_by_role() {

        $args = array(
            'role'         => 'user_defined_role' // Modify to match your needs

        $users_to_delete = get_users( $args );

        foreach( $users_to_delete as $user_to_delete ) :

            // wp_delete_user() accepts another user ID as a second parameter
            // in case you want to reassign the content to an active user
            wp_delete_user( $user_to_delete->ID );


    add_action( 'admin_init', 'wpse220426_delete_users_by_role' );


Remember to backup your database before as it will run straight after activation without the need to trigger any button. Once it has finished deleting the users, also remeber to deactivate o delete it.