Automatically delete WordPress users after specific period of time

Deleting users in WordPress isn’t that hard. If you have to delete only a few of them, you would navigate to user admin pages, select those users worthy of deleting and remove them from your database with one fast click of a finger.

We already showed you a plugin which can easily create, manage and delete WordPress user roles. But sometimes you will need to delete hundreds, even thousands of users. In that case, you can use the previously linked function and do the job without any fuss.

When even that isn’t enough, you will need to program your beloved WP system and allow it to remove users after a certain period of time. Let’s say that you have allowed anyone to register simply in order to participate in a giveaway or to allow them access to specific content for a short period of time. After that time passes, you wouldn’t want all those people to have access to your site. Instead of deleting them one by one or deleting all users in your database, you can use the function we’re about to show you in this article.

Before you start copying and pasting the following function, be sure to create a backup of your database – you never know what can go wrong. Instead of crying about lost user accounts which shouldn’t’ be deleted, always keep a backup.

Install the function and delete users:

The following function will run through all of the user databases and find those who are registered more than specified period of time. In the following example, let’s delete all subscribers who are registered more than 30 days:

  1. Open functions.php file
  2. Copy and paste the functions:
    function auto_delete_users() {
    	global $wpdb;
    	$userlevel = 0; // 0 = subscriber
    	$deleteafter = 30; // delete User after X days
    	$query = $wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users LEFT JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id WHERE $wpdb->usermeta.meta_key = %s AND $wpdb->usermeta.meta_value = %d AND DATEDIFF(CURDATE(), $wpdb->users.user_registered) > %d", $wpdb->prefix.'user_level',$userlevel,$deleteafter);
    
    	if($oldUsers = $wpdb->get_results($query, ARRAY_N)){
    		foreach ($oldUsers as $user_id) {
    			wp_delete_user($user_id[0]);
    		}
    	}
    }
    add_action('daily_clean_database', 'auto_delete_users');
    wp_schedule_event(time(), 'daily', 'daily_clean_database');
    
  3. Change number of days on the 3rd line if you want to
  4. Save changes

After you have made the changes, the function will take care of the time and also take care of user roles. As you can see from the function, we have decided to delete only subscribers (level 0 users) – it wouldn’t be much fun if we had set to delete all users who are registered more than 30 days, wouldn’t it?

That’s all you have to do in order users after a specific period of time. After you’re done with deleting all those users, make sure that you also delete the function from functions.php file – if you leave it there, the function will keep on working every time you reload any WordPress page and you will keep losing your new members if you ever choose to allow new registrations.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

3 thoughts on “Automatically delete WordPress users after specific period of time

Leave a Reply

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