I would like to know if there is any way to automatically purge old unapproved comments on cron run.

The requirement is -- I have turned on email notification on new comments. I would like to approve only the genuine comments and leave the rest for auto-delete on passing certain time.

Of course I already have mollom, captcha and http:BL to fight spam.

1 Answer 1


I'm not sure if there is a module which does that. However, you could do that with the help of some custom coding inside hook_cron:

function MODULE_cron() {
  // Find out the comment ids of the comments that are unpublished
  // and are older than 30 days.
  $comments = db_query('SELECT cid FROM {comment} WHERE status = :status AND created < :created', array(':status' => 1, ':created' => strtotime('today - 30days')))->fetchAll();
  $cids = array();
  foreach($comments as $comment) {
    $cids[] = $comment->cid;

You can change the time to fit your requirements. This will ensure that on cron run the comments which are older than 30 days and are still unpublished get deleted.

  • Sure will do! Thanks for the code snippet. In fact this is what I had thought before posting this question. Want to check if any contrib module is already there as not to duplicate the effort.
    – Sivaji
    Commented Oct 26, 2013 at 15:12
  • 2
    BTW, to make the code even better. The SQL query could have "ORDER by created ASC" and db_query can be replaced with db_query_range($query, 0, 200); to delete the oldest first.
    – Sivaji
    Commented Oct 26, 2013 at 15:16
  • 1
    @AjitS: Shouldn't it be created < :created if you want the comments posted before the specified creation date.
    – wimvds
    Commented Oct 13, 2014 at 14:05
  • @wimvds Good catch! Answer edited.
    – AjitS
    Commented Oct 13, 2014 at 16:33
  • I needed that for Drupal 6, so ported and wrapped it into a D6 module: gist.github.com/amr/cc76bbfbac380996965f Commented Nov 26, 2014 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.