How to Bulk Delete Spam Subscriber Users in WordPress with a Custom Plugin

Several days ago, nearly 5,000 ad subscriber users spammed my WordPress website, even causing the SQL database crash down for a while.

Spam user registrations can become a major headache, especially when your WordPress website is flooded with thousands of fake subscribers overnight. While WordPress allows user deletion through the admin dashboard, it can be cumbersome to handle bulk deletions manually, because you can only delete 20 spam users once. It will take nearly 250 times to delete all spam subscriber users completely.

In this article, we will introduce how to delete all spam subscriber users quickly and efficiently by creating a bulk delete spam users plugin for your WordPress website.

Why Use a Custom Plugin for Bulk Deletion?

A custom plugin gives you complete control over the deletion process. You can filter users by role (e.g., Subscriber) and optionally by registration date. This ensures that legitimate users are not accidentally deleted while removing spam accounts in bulk.

Step-by-Step Guide to Create the Plugin

Step 1. Create the plugin folder and write the plugin PHP file.

  1. Create a folder named it bulk-delete-spam-users
  2. In the bulk-delete-spam-users folder, create a PHP file named bulk-delete-spam-users.php
  3. Copy the code below to bulk-delete-spam-users.php
				
					<?php
/*
Plugin Name: Bulk Delete Spam Users
Description: A simple plugin to bulk delete spam users based on criteria like registration date or role.
Version: 1.0
Author: WPCOLLEGE
*/

// Hook to add admin menu
add_action('admin_menu', 'bulk_delete_spam_users_menu');

function bulk_delete_spam_users_menu() {
    add_users_page(
        'Bulk Delete Spam Users',
        'Bulk Delete Users',
        'manage_options',
        'bulk-delete-spam-users',
        'bulk_delete_spam_users_page'
    );
}

function bulk_delete_spam_users_page() {
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }

    global $wpdb;

    if (isset($_POST['delete_users'])) {
        $role = sanitize_text_field($_POST['role']);
        $date = sanitize_text_field($_POST['date']);

        $query = "SELECT ID FROM {$wpdb->users} u
                  INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id
                  WHERE um.meta_key = 'wp_capabilities' 
                  AND um.meta_value LIKE %s ";

        $params = ["%$role%"];

        if (!empty($date)) {
            $query .= " AND u.user_registered >= %s";
            $params[] = $date;
        }

        $user_ids = $wpdb->get_col($wpdb->prepare($query, $params));

        if (!empty($user_ids)) {
            foreach ($user_ids as $user_id) {
                wp_delete_user($user_id);
            }
            echo '<div class="updated"><p>' . count($user_ids) . ' users deleted successfully!</p></div>';
        } else {
            echo '<div class="error"><p>No users found matching the criteria.</p></div>';
        }
    }

    ?>
    <div class="wrap">
        <h1>Bulk Delete Spam Users</h1>
        <form method="post" action="">
            <table class="form-table">
                <tr>
                    <th scope="row"><label for="role">User Role</label></th>
                    <td>
                        <select name="role" id="role" required>
                            <option value="subscriber">Subscriber</option>
                            <option value="customer">Customer</option>
                            <!-- Add more roles if needed -->
                        </select>
                    </td>
                </tr>
                <tr>
                    <th scope="row"><label for="date">Registered After</label></th>
                    <td>
                        <input type="date" name="date" id="date">
                        <p class="description">Leave blank to delete all users with the selected role.</p>
                    </td>
                </tr>
            </table>
            <p class="submit">
                <input type="submit" name="delete_users" id="delete_users" class="button button-primary" value="Delete Users">
            </p>
        </form>
    </div>
    <?php
}

				
			

Step 2: Save the Code and compress it to.zip file

  1. Compress the folder bulk-delete-spam-users  into a .zip archive if you plan to upload it via the WordPress admin panel.

Step 3: Install and Activate the Plugin

  1. Log in to your WordPress admin dashboard.

  2. Go to Plugins > Add New and click Upload Plugin.

  3. Upload the bulk-delete-spam-users.php file or its .zip version.

  4. Activate the plugin.

Step 4: Use the Plugin to Delete Users

  1. Navigate to Users > Bulk Delete Users in the WordPress admin panel.

  2. Select the User Role (e.g., Subscriber).

  3. Optionally, specify a Registered After date to delete only users registered after a specific time.

  4. Click Delete Users.

The plugin will process the deletion and display a success message showing how many users were deleted.

The advantages of this bulk delete spam users plugin

  • Time-Saving: Deletes thousands of spam users in just a few clicks.

  • Customizable: Filters users by role and registration date, preventing accidental deletion of legitimate users.

  • No Third-Party Dependencies: Avoids the need for external plugins or services.

Conclusion

Bulk delete spam user plugin can help you delete flooded spam users quickly and efficiently. However, preventing spam users is the best way to secure your WordPress website. If your website does not disable user registration, we recommend 2 strategies for your website.

  1. Enable CAPTCHA: Add CAPTCHA to your registration forms using plugins like Simple Cloudflare Turnstile.

  2. Email Verification: Require users to verify their email before completing registration.

By this way, you can keep your WordPress user list clean and manageable.

Bulk Delete Spam Users

Bulk Delete Spam Users Plugin

Save time when deleting flooded spam users on the WordPress Website. Quickly and efficiently bulk deletes spam ad users. Delete different groups of spam users by filtering their user roles, preventing accidental deletion of legitimate users.

Scroll to Top