I have a cron job where I need to do a node_delete().

From what I can tell the delete occurs okay if I run the cron job manually from the status report page, but if I try to run the cron from inside a call to a module_cron function it looks like the delete fails.

Is there some way to give the cron the access to delete nodes with node_delete?

  • 1
    As a note, I (we?) would recommend that you accept answers where such questions are answered sufficiently. This will benefit both you and the community. – jordojuice Sep 19 '11 at 23:40

A scheduled cron runs as uid 0, the anonymous user, and therefore probably can't delete anything. When you run cron from the admin menu, it uses your current session, so you can delete things.

If you google for "drupal run cron as uid 1" you will see some workarounds. Most involve switching users inside your hook_cron and then restoring the session when you are done.

What should be possible is to invoke drush -u XXX cron from your crontab instead of the the typical wget method. Some searching will dig up the proper way to call it (you need to get the environment and Drupal basedir set right). I would not run as uid 1, though. Pick (or create) a user with sufficient permissions to do what you need.

What would also be possible is to create a script that bootstraps, loads the proper user, and then does your task. Lock this down in your .htaccess and call it from your crontab.


For deletion of a node by using hook_delete, it is necessary that the author(user) of that node is present in the db. To achieve consistency, one should delete nodes first and then its user. If you donot want to delete node but only want to delete a user you should use hook_user and in delete case, set uid of the node to 1, as uid = 1 is for super admin. So we will not face orphaned nodes in the database. Hope so, it will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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