Mass removing spam wordpress users

Shock horror, an actual post!

I’ve been slowly (very, very slowly) redoing this blog now on and off for ages. Nearly finished, it was time for a clear up of old content/spam etc etc. first place i went was to the spam users. I had 1582. No way am i clearing that up 20 by 20. I had a quick google and it turns out you can change the screen options to up to 999, but it seems that wont work because the header string becomes too long and crashes out.

Then i found this article.

basically, all you need to do is put the following code in your functions.php file, upload and refresh your site. it’ll tell you how many users its removed, then you just remove the code from your functions. simple.

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 ) ) {
echo $i.' Subscribers deleted';
} else {
echo 'No Subscribers deleted';

Leave a Reply

Your email address will not be published. Required fields are marked *