• Resolved mplusplus

    (@mplusplus)


    Hi there

    Using WordPress PHP code, how to bulk delete ONLY 100 subscribers at a time from thousands of users?

    (The following code tries to delete all 50k users at once and my server hangs. If I can delete only 100 users at a time then I can use a Cron job every 5 minutes.)

    <?php
    $blogusers = get_users( ‘role=subscriber’ );
    // Array of WP_User objects.
    foreach ( $blogusers as $user ) {
    $user_id = $user->ID;
    wp_delete_user( $user_id );
    }

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Ian Sackofwits

    (@plantprogrammer)

    Hi, mplusplus, I think the following tutorial page should be helpful in addressing your issue: https://www.w3schools.com/php/php_looping_for.asp

    Since get_users returns a numerically indexed array, you can just reference the current element in the array like the following: $blogusers[$i] where i would be in the current counter for the loop.

    Dion

    (@diondesigns)

    I believe that the following change to your query:

    $blogusers = get_users(‘role=subscriber&number=100’);

    will do what you want.

    Thread Starter mplusplus

    (@mplusplus)

    @diondesigns That worked like a charm! Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using WordPress PHP code, how to bulk delete 100 subscribers at a time?’ is closed to new replies.