• I am trying to delete a large number of users and finding it to run very slow. I am using a get_users followed by a simple loop calling wp_delete_user. It seems to be taking 9-15 seconds per user. With thousands of users to delete, this could take days.

    Is there a way to speed it up?

    <?php
    define( 'WP_USE_THEMES', false );
    require( './wp-load.php' );
    $args = array(
    		'number' => 50,
    		'role'   => 's2member_level1' // Modify to match your needs
    );
    $reassignto = 1618;
    $users_to_delete = get_users( $args );
    require_once( ABSPATH.'wp-admin/includes/user.php' );
    foreach( $users_to_delete as $user_to_delete ) :
    		wp_delete_user( $user_to_delete->ID, $reassignto );
    endforeach;
    

    BTW-I had tried a few plugins and found them to be even slower.

Viewing 2 replies - 1 through 2 (of 2 total)
  • robertolokoloko

    (@robertolokoloko)

    An easy way doin it, is directly from phpmyadmin, acces wp_users and than you know the rest i hope lol. About the delay hmmmm smth else going on backstage there

    • This reply was modified 8 years ago by robertolokoloko. Reason: missspelling
    Moderator bcworkz

    (@bcworkz)

    If you want to do this efficiently by executing code, as opposed to Robert’s suggestion of using phMyAdmin (a great idea actually), compose a mySQL query that drops all user records where the related user ID in usermeta has meta_key = “wp_capabilities” and the corresponding meta_value is LIKE “%s2member_level1%”. You can run the query through $wpdb->query(). Test carefully using LIMIT before you unleash your unlimited query on the entire DB! You could also use $wpdb->delete() to a similar end.

    Once the user records are dropped, do something similar for the related usermeta records. This is assuming the deleted users have not authored any posts. If they have, either delete their posts or change the author ID to another user.

    If your server is actively being used, you could prevent your queries from bogging down the server by limiting drops to perhaps a few hundred records at a time. Repeat until no users of that role are found. This could be run hourly as a WP scheduled event. Once no records are found the script can unschedule itself. The whole thing could be made into a plugin. Activate it to start the process, then remove or deactivate it once it’s done

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘deleting large # of users performance issue’ is closed to new replies.