How To Use wp-cli To Delete Old Subscribers (Wordpress)

Donovan Nagel
Written by Donovan Nagel

I have a Wordpress paid membership site (s2member) with hundreds of thousands of paid and free members.

After a massive server upgrade recently and optimization, I needed a way to conveniently purge the database of 6 years worth of inactive free members.

Previously I was using a custom plugin for this running a cron job but for some reason it stopped working, and I need a simpler method to SSH in and purge as required.

Plus there was the issue of bbPress which I removed ages ago. It left a mess behind which I needed to clean up.

wp-cli has been incredibly useful for all this.

Removing bbPress roles

bbPress is like herpes. Once you get it on your Wordpress installation, you can never seem to get rid of it. 😂

For my large membership site, I was originally using bbPress as a community forum (I’ve since switched to Discourse ).

But removing bbPress has been an ongoing nuisance.

One frustration is that it leaves behind useless roles after deletion (Spectator, Participant, Moderator, Blocked, Keymaster).

You can remove these roles instantly by putting the following into your functions.php file:

remove_role( 'bbp_blocked' );
remove_role( 'bbp_moderator' );
remove_role( 'bbp_keymaster' );
remove_role( 'bbp_participant' );
remove_role( 'bbp_spectator' );

This is fine, but here’s the problem:

If any of your users (including Admin) have one of these roles when you delete them, you’ll break your permissions. Administrator (with Keymaster role) user will no longer be able to access the Wordpress dashboard, and the only way to get access again will be to add the roles back.

To get around this, you’ll need to remove the roles from users BEFORE removing the roles from Wordpress.

Use this command to remove a role from a user:

$ wp user remove-role <USER ID> bbp_keymaster

You can also use the plugin User Role Editor .

Once all your users are clear of the bbPress roles, you can go ahead and modify the functions.php file.

To list current users who have bbPress roles:

$ wp user list --role=bbp_participant
$ wp user list --role=bbp_spectator
$ wp user list --role=bbp_keymaster

To count how many there are, pipe into wc:

wp user list --role=spectator | wc -l

As I said above, to clear out old bbPress roles (keymaster, spectator, participant, etc.):

$ wp user remove-role <USER_ID> bbp_participant

Here’s where wp-cli is cool: instead of using a user ID, you can actually use another wp-cli command.

To give you an example of what I mean, here’s my short script to purge Wordpress subscribers by registration date (oldest first and limited to a 100 at a time with a rest in between runs):

for i in {1..50}
        printf "\033[1mPurging 50 shitty users...\033[0m\n\n"
        wp user delete --yes $(wp user list --role=subscriber --fields=user_email,user_registered --orderby=user_registered --order=asc | head -n 100)
        printf "\033[1mWaiting 10 seconds before starting over...\n\n"
        sleep 10
        printf "Done. Starting next purge...\033[0m\n\n"

Here’s the command, nested inside another command:

$ wp user delete --yes $(wp user list --role=subscriber --fields=user_email,user_registered --orderby=user_registered --order=asc | head -n 100)

So basically, my script here is taking a list of Wordpress users (ordering by registration date and role with a max of 100 at a time, in this case), and running over at that list with the wp user delete command.

The for loop can be replaced with a while loop to run forever, or just change the number of loops.

You can set this up as a cron job to run, let’s say, once a month.

Taking this approach, I was able to clean out a massive Wordpress database of users and purge the annoying bits and pieces left behind by bbPress.

wp-cli is a super handy and powerful way to manage a Wordpress installation entirely over SSH.