Deleting non-active users (e.g., users who didn't log in for 90 days)

by WestieUK   Last Updated September 17, 2018 22:07 PM

We use Drupal as an employee portal; as such, when new starters join the business they create an account.

However, we are trying to figure out a good leaving process; right now I get a list and have to manually delete all the users.

Ideally, we would like something which send an email to a user after 14 days and then again after 30 days informing them that they have not logged in into the system; then, after 60 days, they are automatically deleted.

I am guessing that using Rules is the answer, but I cant find any option to delete users.

Tags : 6 users rules

Answers 5

Maybe this module can help: inactive user

From the module page:

After configurable periods of inactivity, one or more of the following actions can be automatically taken:

  • send an email to the user
  • send an email to the site administrator(s)
  • block the account (a warning can first be issued)
  • optionally notify user and/or site administrator(s) that account was blocked
  • delete the account (a warning can first be issued)
  • optionally notify user and/or site administrator(s)1 that account was deleted
August 03, 2011 14:12 PM

If the Rules module doesn't have a predefined action that allows deleting a user, then you can choose the action that executes the code necessary to find the users that didn't log in the past 90 days, and deletes them.

You can create a rule that is executed when Cron maintenance tasks are performed (that is one of the events you can select when creating a rule), and add an "Execute custom PHP code" action.

$query = db_query_range("SELECT uid FROM {users} WHERE access < %d", time() - 7776000, 0, 20);

while ($uid = db_result($query)) {
  user_delete(array(), $uid);

return array();

The result would be something similar to the following one:


The return statement is probably not necessary, but as I understand, the Rules module is expecting the code returns an array containing something that needs to be saved; the code could return a node that needs to be saved, for example, and which will be saved if the node has been modified.
The 7776000 value I used is the number of seconds in 90 days.

This is the solution I would adopt if you are already using the Rules module. If you are going to install it just to be able to accomplish this task, then it would be better to use a custom module that implements hook_cron(), or using a module that already does the task.
Which one to choose depends from what you plan to do with the module you are going to install; if you are using few of the module's features, then a custom module is probably a better solution.

August 03, 2011 14:32 PM

I also use Views Bulk Operations for this. I set up a query that does what I want, and then execute the "delete user" action. Before doing that, I do a "send email" action to tell them why they're being deleted.

August 03, 2011 15:28 PM

Just for those, looking for that straight forward approach in drupal 6:

"SELECT uid FROM {users} WHERE access > %d"

should be

"SELECT uid FROM {users} WHERE access < %d"

to delete users accessing the system more than 90 days ago.

June 03, 2013 08:39 AM

Been in search how to complete similar task. Found an answer - if your Rules don't have 'Delete user' option then you have to install Views Bulk Operations module - it provides such operation (and lots of others :) Of course, it can be done with custom PHP code in different ways, but this solution is useful if your site has disabled PHP input for security reasons (and you don't want to write the action by yourself).

February 04, 2014 16:34 PM

Related Questions