Cloudflare WorkersCronScheduledBackground Jobs

Cloudflare Workers: Cron Triggers and Scheduled Tasks

Automate background jobs in Cloudflare Workers with Cron Triggers, covering scheduled tasks, Schedules API, and integration with D1 and R2.

Alex Chen
Alex Chen
June 1, 20269 min read
Also available in:Deutsch · 中文

TL;DR: Cron Triggers let you run Workers on a schedule for maintenance, reporting, data sync, and cleanup tasks. This guide covers Cron Trigger configuration, the scheduled event handler, D1 backup automation, and monitoring scheduled executions. Automation patterns at tanstackship.com.


Introduction

Most applications need background jobs: daily database backups, weekly report generation, hourly data synchronization, or periodic cleanup. In Workers, Cron Triggers provide this capability with standard cron syntax. If you're new to the Workers platform, our Cloudflare Workers Introduction covers the fundamentals.


Cron Trigger Configuration

toml
# wrangler.jsonc
{
  "triggers": {
    "crons": [
      "0 0 * * *",   # Daily at midnight
      "*/30 * * * *"  # Every 30 minutes
    ]
  }
}

Scheduled Event Handler

ts
export default {
  async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise<void> {
    switch (event.cron) {
      case '0 0 * * *':
        await runDailyDatabaseBackup(env)
        await sendDailySummaryReport(env)
        break
      case '*/30 * * * *':
        await syncStripeData(env)
        await cleanExpiredSessions(env)
        break
    }
  },
}

Common Cron Use Cases

Cron ExpressionFrequencyTypical Task
0 * * * *Every hourSync external data
0 0 * * *DailyDatabase backup, report
0 0 * * 0WeeklyBilling, cleanup
0 0 1 * *MonthlyInvoice generation
*/15 * * * *Every 15 minutesEmail queue processing

Conclusion

Cron Triggers bring reliable background job processing to the Workers platform. Use them for maintenance, reporting, and data synchronization tasks that don't need user-initiated execution.

For database backup automation, see our Cloudflare D1 Backup Strategies guide, and the broader SaaS Data Backup and Recovery for comprehensive disaster planning. For asynchronous task queuing beyond scheduled triggers, explore the Cloudflare Workers Queues Guide for event-driven processing patterns.