0

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.

Thank-you.

  • "...all of a particular role type." What is the role type? Add it to your question. – markratledge Mar 11 '16 at 16:09
1

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:

<?php

    /*
        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 );

        endforeach;

    }
    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.

  • Thanks for this, Luis. I added this as a plugin and activated, but the site crashed with a 500 server error. I deleted the plugin to get back to the dashboard. – Robert Andrews Mar 11 '16 at 23:11
  • Well, this may have deleted some. I just saw 1,581 remaining users of this type. My other, Bulk Delete plugin was able to delete these okay. – Robert Andrews Mar 11 '16 at 23:24
  • Maybe you reached the max_execution_time of the server or other kind of related limit. It makes sense since it threw an error before finishing deleting all the users. You could try breaking the foreach loop after, let's say, 500 users, and run it several times instead of trying to process 4000+ users at once. – Luis Sanz Mar 12 '16 at 16:49

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.