8.5.x core.api.php hook_cron()
8.0.x core.api.php hook_cron()
8.1.x core.api.php hook_cron()
8.2.x core.api.php hook_cron()
8.3.x core.api.php hook_cron()
8.4.x core.api.php hook_cron()
8.6.x core.api.php hook_cron()
4.6.x core.php hook_cron()
4.7.x core.php hook_cron()
5.x core.php hook_cron()
6.x core.php hook_cron()
7.x system.api.php hook_cron()

Perform periodic actions.

Modules that require some commands to be executed periodically can implement hook_cron(). The engine will then call the hook whenever a cron run happens, as defined by the administrator. Typical tasks managed by hook_cron() are database maintenance, backups, recalculation of settings or parameters, automated mailing, and retrieving remote data.

Short-running or non-resource-intensive tasks can be executed directly in the hook_cron() implementation.

Long-running tasks and tasks that could time out, such as retrieving remote data, sending email, and intensive file tasks, should use the queue API instead of executing the tasks directly. To do this, first define one or more queues via a \Drupal\Core\Annotation\QueueWorker plugin. Then, add items that need to be processed to the defined queues.

Related topics

15 functions implement hook_cron()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

aggregator_cron in core/modules/aggregator/aggregator.module
Implements hook_cron().
comment_cron in core/modules/comment/comment.module
Implements hook_cron().
common_test_cron in core/modules/system/tests/modules/common_test/common_test.module
Implements hook_cron().
common_test_cron_helper_cron in core/modules/system/tests/modules/common_test_cron_helper/common_test_cron_helper.module
Implements hook_cron().
dblog_cron in core/modules/dblog/dblog.module
Implements hook_cron().

... See full list


core/core.api.php, line 1893
Documentation landing page and topics, plus core library hooks.


function hook_cron() {

  // Short-running operation example, not using a queue:
  // Delete all expired records since the last cron run.
  $expires = \Drupal::state()
    ->get('mymodule.cron_last_run', REQUEST_TIME);
    ->condition('expires', $expires, '>=')
    ->set('mymodule.cron_last_run', REQUEST_TIME);

  // Long-running operation example, leveraging a queue:
  // Fetch feeds from other sites.
  $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time AND refresh <> :never', array(
    ':time' => REQUEST_TIME,
  $queue = \Drupal::queue('aggregator_feeds');
  foreach ($result as $feed) {


ghalenir’s picture

1. How do we run custom cron from drush. Other than drush core-cron, I didn't find the way to run cron from drush.
2. I also think that we removed this api from drush 8 http://www.drupalcontrib.org/api/drupal/contributions!drush!docs!drush.a...