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 methods of automating this task.

There are many situation in which we might need to delete or clean up our WordPress users accounts. This might be due change of strategy or perhaps, we need to reduce our database size. Or maybe we need to simply remove the clutter of old and inactive WordPress user accounts. For whatever reason, there are some cases when we need to delete WordPress accounts.

A cleanup can create a lot of good side effects. An overestimated user base for example might water-down our efficiency metrics. For instance, we may think that some long-term decisions didn’t have the effect we wanted. But you are in fact making decisions with outdated data and count user account that are not active anyways.

Another good side effect when you delete WordPress accounts is the cost reduction. Even though data storage is virtually free, data processing might be expensive. Sending emails, creating metrics, creating ads. All these actions have their costs, which can be higher if they fall on deaf ears.
When it comes to implementing, the main issue is that these cleanups aren’t global. After all, we want to keep records for our active users. Then we need to find ways to efficiently tag and delete just the user accounts we want.
That’s when Users Insights comes 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!

delete wordpress account in bulk

How to find the WordPress accounts to delete

To bulk delete the users, we would need to go through the following process:

  1. 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
  2. Either directly export the filtered users or mark them as a “cleanup” group to export them later
  3. Import the exported user emails to the Bulk Delete Users By Email plugin

When it comes to filtering our users, we need to define what an inactive user is. There are many ways to define inactive users. And 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 who has been registered before a specified date. You can alternatively apply a day based filter such as “Date registered is more than 365 days ago”.

Filter WordPress users by date registered

But old users aren’t always inactive users. In fact, 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 detects, such as geolocation, page views and sessions
  • 3rd party plugins data – Users Insights supports some of the most popular 3rd party plugins. So, you could filter your users based on other activity, such as number of WooCommerce orders, or number of topics/replies with bbPress, or 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 use a combination of Date Registered + Number of orders. But once again, you can use any of the fields that work best for your specific case.
Find WordPress users to delete by activity

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 even filter only users with no orders?

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.

Another interesting filter is a weird loophole that was caused mostly by GDPR (or simply by older sign-up methods). Now we need the express consent of our users in order to get in touch with them. And if you didn’t collect the user consent prior to the change, these user emails might be in fact useless. You have them, but you can’t really do anything with them.

If you are storing the consent given as a user meta with 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.
Delete WordPress account with no GDPR acceptance

Moreover, you may be going on structural changes in your site. For instance, if you have a free plan that won’t exist anymore. Or if you are killing a product line. In this case, you might be able to filter out the users to be deleted by using the role field:
Find WordPress users to delete by role

Now that we have found the user accounts to delete we have two options. Easiest option would be to just export the filtered users. This can be done by clicking on the Export button, once the filters are applied. When exporting, make sure that the Email column is visible by clicking on the eye-icon menu. In this way, the email will be included in the export list.

Export WordPress users to delete

Another method would be to add the users to a cleanup group and then export all the users who belong to this group. You might prefer this method if you need to apply separate filters to find the users. Or you might want to just manually mark users for cleanup over time and then perform all the deletion at once.

To add the filtered users to a group, just select all the users on the current page and then click on the Bulk Actions -> Add to group button. If you have multiple pages of users, repeat the same for the rest of the pages.

Assign bulk delete users group

Now that we have all our cleanup users, we can simply export this full list. You can do so by filtering all users in the cleanup group. Once again, make sure to have the e-mail column visible when exporting the user list.
Export bulk delete user emails

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

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 E-Mail plugin.
Bulk delete users via email

We play it safe here. Therefore, the recommended procedure is to:

  1. Copy a few emails from your CSV
  2. Paste them in the plaintext field of the Bulk Delete Users plugin
  3. Run the delete operation
  4. Check if this works well, and just for the users you wanted
  5. 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:
Confirming results of user delete operation

How to delete WordPress users via code

In this example, we’ll show you how to bulk delete the users who are assigned to a “cleanup” 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 is going to work better if you want to delete all the user data, posts, 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 add re-assign 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, orders data, forum posts, or even actual blog posts if they have them.

This is the code we can use now:

You can paste this code in 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 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 saw how to delete WordPress accounts. In addition, we investigated the importance of a user cleanup, and how it can be beneficial for your site. Then we ran two different options for bulk user deletion.

We hope you enjoyed this tutorial! See you again next time.