WordPress To Delete All Subscribers, Bulk Delete Of Spammy Users Based On Role
You’ll be happy to know that this post will give you the information to do the job. It’ll help you bulk delete that chunk of spammer subscribers from your WordPress blog. Finally right? I’d agree.
Now, keep in mind, that I did find this code elsewhere but I won’t be linking to the post because for one, the solution was in a comment and 2, it messed up my site just a bit and for about 10 minutes there was a 500 server error displaying. I believe this is because the job of deleting was happening in the background and I should have left the site alone. After stumbling for a way to fix the 500 server error I found when navigating to the home page, the WordPress install page was displaying. That’s not cool.
Either way it worked. Among all the momentary turmoil I endured the 15,000 plus spammy subscribers on this site are now gone. I also installed the Sabre spam fighter plugin and so far it works like a charm. I’ll comment in the next few days if I see the spam fighter plugin doesn’t work as expected but with a very configurable registration captcha system for the registration page, I can’t really see how I could go wrong. We’ll see.
So now for the code. I apologize for writing like a spammy Internet marketer but this piece of code needed an introduction. Use at your own risk. I reckon you have nothing to worry about as everything turned out just fine here.
function remove_subscribers() {
global $wpdb;
$args = array( 'role' => 'Subscriber' );
$subscribers = get_users( $args );
if( !empty($subscribers) ) {
require_once( ABSPATH.'wp-admin/includes/user.php' );
$i = 0;
foreach( $subscribers as $subscriber ) {
if( wp_delete_user( $subscriber->ID ) ) {
$i++;
}
}
echo $i.' Subscribers deleted';
} else {
echo 'No Subscribers deleted';
}
}
remove_subscribers();
Throw the above into your functions.php file and visit any page on your website. Let the script run and at the end it should display the number of subscribers that were deleted. After you’ve run it go and check your WordPress users to see if the script worked. Then delete the script from functions.php and install Sabre. From there you should be fine.
Now, I’m wondering if someone will make a plugin out of this. I hope so because I’m just too busy to get started on another project.