How to bulk delete WordPress user accounts
In this tutorial, we’ll look into different methods to bulk delete WordPress user accounts. In addition, we investigate the benefits of cleaning your database, and we find ways of automating this task.
There are many situations in which we might need to delete or clean up our WordPress users’ accounts. This might be due to a change of strategy, or perhaps we need to reduce our database size. Or maybe we need to remove the clutter of old and inactive WordPress user accounts. For whatever reason, there are some cases when we need to delete WordPress accounts.
When it comes to implementing, the main issue is that these cleanups aren’t global, meaning we don’t want to delete all users at once but just a fraction of them. After all, we want to keep records of our active users. Then, we must find ways to tag and delete just the user accounts we want efficiently.
That’s when Users Insights come into play. With it, we can filter the users we want exactly. Then, we can tag and delete them using other plugins.
Let’s get started!
What is Bulk Delete?
Unlike manual user deletion, bulk delete is a method that allows you to delete multiple users at once. This can be useful when you need to delete a large number of WordPress users and their data from the database. You can also use bulk delete to delete meta fields, user taxonomies, and user roles. But before we proceed with our bulk deletion process, we first need to get a list of all users that we want to delete.
How to find the WordPress accounts to delete
To bulk delete the users, we would need to go through the following process:
- Find the users to delete. You can apply different filters to find your inactive users. This could be a combination of any of the supported fields and filters
- Either directly export the filtered users or mark them as a “clean up” group to ship them later
- Import the exported user emails to the Bulk Delete Users By Email plugin
When filtering our users, we need to define what an inactive user is. There are many ways to define inactive users. Only you know for sure which definition works the best for your business. In our example, we use the user registration date in combination with a lack of orders. In addition, we can also filter our subscribers who don’t want to get any emails.
Now, let’s see a few examples.
To filter our users, let’s get everyone registered before a specified date. You can alternatively apply a day-based filter such as “Date registered is more than 365 days ago”.
But old users aren’t always inactive users. Sometimes, we are lucky to find loyal customers early on. Thus, we might need to apply additional filters to find the inactive users. Users Insights supports filtering by various user information, such as:
- Default WordPress user data, such as registration date, role, user meta fields, and number of posts and comments created
- Additional data that Users Insights detect, such as geolocation, page views, and sessions
- 3rd party plugin data – Users Insights supports some of the most popular 3rd party plugins. So, you could filter your users based on other activity, such as several WooCommerce orders, topics/replies with bbPress, or the last activity date with LearnDash. Or perhaps you might want to delete all users who don’t have an active membership.
In our example, we’ll combine the Date Registered + Number of orders. But once again, you can use any fields that work best for your case.
You may question yourself now: “If we want to delete WordPress accounts that are inactive, why should we keep old customers?”. In other words, why should we filter only users with no orders and not users who haven’t logged in for a while?
Well, this may be simple, but often, we can win back lost customers. If a user has ordered in the past, there’s a chance that we can win them back, and this information is too valuable to throw away. This is why we want to delete WordPress users with specific characteristics, and for this, we need to be able to select only the users you want to delete.
Another interesting filter is a weird loophole caused mainly by GDPR (or older sign-up methods). Now, we need the express consent of our users to get in touch with them. And if you didn’t collect the user consent before the change, these user emails might be, in fact, useless. You have them, but you can’t do anything with them.
If you store the consent given as a user meta with the value of Yes or No, you can use the custom fields feature to load these values in the table and filter the users based on this field.
How to Bulk Delete Users in WordPress based on their Roles
Bulk deleting WordPress users based on their role can be useful in various scenarios, such as cleaning up after temporary events, expiring memberships, removing spam or inactive accounts, managing role changes, and enhancing security by limiting access for certain roles.
Moreover, you may be going through structural changes in your site. For instance, if you have a free plan that won’t exist anymore. Or if you are discontinuing a product line. If you had a separate user role for this type of users and want to delete users with specific roles, you can filter out the users to be deleted by using the role field:
Now that we have found the user accounts to delete, we have two options. The easiest option would be to export the filtered users. This can be done by clicking the Export button once the filters are applied. When shipping, ensure the Email column is visible by clicking on the eye-icon menu. In this way, the email will be included in the export list.
An alternative would be to create a cleanup group and add the relevant users before exporting them. This might be your preferred option if you need to use different filters to locate the users or if you would instead manually flag users for cleanup and then delete them all in one go.
To add the filtered users to a group, select all the users on the current page and click the Bulk Actions -> Add to group button. If you have multiple pages of users, repeat the same for the rest of the pages.
Now that we have all our cleanup users, we can export this full list. You can do so by filtering all users in the cleanup group. Once again, make sure to have the email column visible when exporting the user list.
Take the time to review this list and double-check if you don’t have any unwanted users flagged for deletion. After this, write down the total number of users you have and the size of your cleanup group. This can be used to double-check if no unwanted users have been deleted.
How to bulk delete WordPress accounts with a plugin
We can’t stress this enough, but right now, you should do a full backup. Even if it’s a test server. Or even if your hosting provider has automated backups. It doesn’t even matter if this is just sample data. A simple backup takes, at worst, a few minutes. But if you lose just a bit of important data, that may never be recovered.
Then, install the Bulk Delete Users by Email plugin.
We play it safe here. Therefore, the recommended procedure is to:
- Copy a few emails from your CSV
- Paste them in the plaintext field of the Bulk Delete Users plugin
- Run the delete operation
- Check if this works well and just for the users you wanted
- Repeat until you finish
This is important to make sure that you don’t delete unwanted users. Additionally, when you work in batches, you won’t risk a timeout from your server. When your server times out, the operation is interrupted, and the results are unpredictable.
Once your delete operations are complete, you can check the results of your work. For that, you can go back to Users Insights and filter your group once again. You should see no results:
How to bulk delete WordPress users programmatically via code
In this example, we’ll show you how to bulk delete the multiple users who are assigned to a “clean up” group with a bit of code.
Again, we can’t stress this enough, it’s time for a backup. This time, it’s particularly crucial because once the code is running, we can’t stop it.
This solution will work better if you want to delete all the user data, posts, and metadata. That’s because the function we are using runs both a delete_user command and a delete_usermeta command.
In addition, you can use the reassign parameter to reassign the user’s posts to another user. In our case, we are reassigning it all to the user with ID 1. If you leave it blank, these posts are deleted as well. This affects, for example, order data, forum posts, or even actual blog posts if they have them.
This is the code we can use now:
<?php function my_delete_users() { //if you aren't using cleanup, replace with your own group slug $slug = 'cleanup'; $taxonomy = 'usin_group'; $term = get_term_by( 'slug', $slug, $taxonomy, 'ARRAY_A' ); $id = $term['term_id']; $users_to_delete = get_objects_in_term( $id, $taxonomy ); if( ! empty( $users_to_delete ) ) { foreach ( $users_to_delete as $uid) { //delete users and reassign their posts to the user with ID 1 //(admin in our case) wp_delete_user( $uid, 1 ); } //just so you know what is going on echo "<script>alert('Cleanup code run');</script>"; } } add_filter('admin_footer', 'my_delete_users');
You can paste this code into your child theme’s functions.php file. Visiting any of the admin pages will trigger the script. You can alternatively use a different hook than admin_footer to control when and how the script is executed. Once it is run, you will see a “Cleanup code run” dialog notifying you that the script has finished. After this, you can remove the code from the functions.php file.
These are the important bits:
- $slug = ‘cleanup’; – Here, you can change the group slug. Make sure that it matches the group you want to delete
- $term = get_term_by(‘slug,’ $slug, $taxonomy, ‘ARRAY_A’); – This function simply gets the data for our slug so that we can find its ID
- $users_to_delete = get_objects_in_term( $id, $taxonomy ); – Here we load all users for that group ID
- wp_delete_user( $uid, 1 ); – delete the user and assign the user data to the user with ID 1 (in our case, the admin user)
Conclusion
Today, we explored the process of bulk deleting WordPress accounts, which involves removing multiple user accounts from a WordPress website simultaneously. This action is commonly undertaken to streamline user database maintenance, ensuring that the user list remains accurate, relevant, and secure.
During our discussion, we delved into the significance of user cleanup and how it can enhance the overall performance of your site. We also looked at the various criteria on which bulk deletion can be based, including user roles, membership status, activity, and other specific attributes.
Furthermore, we explored two distinct methods for bulk deleting numerous users at once. It’s important to exercise caution when performing such deletions to prevent data loss and to adhere to best practices for user data protection and privacy.
We hope you found this tutorial informative and useful! Stay tuned for more insights in our future discussions.