Deleting / Purging Cache, Users, Nodes, Comments (Housekeeping)

If you have developed a drupal site and are maintaining it too, you will soon find that your database will keep growing with lot of unwanted posts, comments and user registrations. There is a need to purge this data from time to time to keep storage levels at optimum and also your website updated with only relevant content.

I was looking at what could be the different ways in which you can clear data / Purge data in drupal.  I have logically divided it into 4 categories

  1. Purging Cache
  2. Purging Users
  3. Purging Comments
  4. Purging posts

Lets look at each one of them

  1. Cache actions provides rules actions for clearing drupal caches. It currently provides actions for:
  • Clearing Drupal cache bins
  • Clearing CSS/JS cache
  • Clearing The cache of specific views
  • Clearing The cache of specific panel pages
  • Clearing The cache of specific mini panels


  1. The Cleaner module allows the admin to set a schedule for clearing caches, watchdog, and old sessions. Its available only for Drupal 5 and Drupal 6

There are functions in Drupal which will cause "expired" entries in some cache tables to be deleted. This is vastly improved in Drupal 6. "Minimum_cache_lifetime" is a partial solution, but still not totally complete.  There are still times and/or cache tables that don't get cleared in any of those scenarios. Many sites will not be impacted by this, but a few will (just search on and you will see many posts from people having problems).

  1. Also refer to this documentation on clearing cache
  2. The LoginToboggan module offers several modifications of the Drupal login system in an external module by offering the following features and usability improvements: Optionally have unvalidated users purged from the system at a pre-defined interval (please read the CAVEATS section of INSTALL.txt for important information on configuring this feature!).
  3. The inactive_user module provides Drupal administrators with a way to automatically manage inactive user accounts. This module has two goals: to help keep users coming back to your site by reminding them when they've been away for a configurable period of time, and to cleanup unused accounts.
  4. User Prune lets you mass delete inactive users based on criteria you specify.

The module classifies inactive users into two categories: users who have never logged in before, and users who have logged in at least once. For users that have never logged in before, you can choose to prune users based on how long they've been registered. For users that have logged in before, you can chose to prune users based on both how long they've been registered, and how long its been since they last logged in. The pruning specification you select can be saved as a cron job, or executed a single time.

  1. Deleting nodes at the database level is a bad idea because there are so many tables in the database that contain node-related data. If deleting nodes in bulk at admin/content/node is not adequate for your needs then you need to make use of the node delete api function. Example usage to delete page type nodes.

$node_type = 'page';


//fetch the nodes we want to delete

$result = db_query("SELECT nid FROM {node} WHERE type='%s'",$node_type);

while ($row = db_fetch_object($result)){




//simple debug message so we can see what had been deleted.

drupal_set_message("$deleted_count nodes have been deleted");


  1. To delete duplicate nodes  refer to this code snippet
  2. Deleting Comments – If you have to delete specific comments or spam comments you can use views bulk operations to mass delete comments Install the views bulk operations module, create a page view of comments, and choose "Bulk Operations" as the view style. Make sure you select "Delete comment" as one of the selected operations. When you view the view page you created, you'll be able to bulk delete comments.

  3. Deleting nodes can be done using Views bulk operations module in the same way as above.