2

I have this code in my module_cron function (I need to delete old unpublished nodes).

include_once './modules/node/node.module';
$nids = db_query("SELECT nid FROM node where status = 0 AND UNIX_TIMESTAMP()-changed>172800 LIMIT 2000");
while ($result = db_fetch_object($nids)) {

  echo  "Deleting node ".$result->nid."<br>";
  node_delete($result->nid);    
}

It always outputs node ids, but it rarely actually deletes nodes. In fact it seems to totally stop deleting them until I login to admin panel. What am I doing wrong? Is there any way to always delete nodes?

3 Answers 3

1

Check this "Running cron as an authenticated user" in http://drupal.org/node/23714 it might be useful for you.

1

Here's one solution:

  include_once './modules/node/node.module';                         
  $nids = db_query("SELECT nid FROM node where status = 0 AND UNIX_TIMESTAMP()-changed>172800 LIMIT 2000");    
  global $user;
  $orig_uid = $user->uid;
  $user->uid = 1;    
  while ($result = db_fetch_object($nids)) {

      echo  "Deleting node ".$result->nid."<br>";
      node_delete($result->nid);    
  }
  $user->uid = $orig_uid;
0

There might be the issue with the permissions. Please check the permissions of deleting the desired content type's content as cron might be running as anonymous user.

1
  • That's exactly the problem, thanks! So how do I run cron as admin? I don't want anonymous users to be able to delete nodes Commented Jun 14, 2012 at 14:09

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.