Scheduled Tasks & Crons
Some tasks need to run on a schedule — checking for updates, generating reports, cleaning up data. Cloudflare Workers Cron Triggers make this serverless and free.
Cron Triggers on Cloudflare Workers
Section titled “Cron Triggers on Cloudflare Workers”A Worker can be triggered on a schedule instead of (or in addition to) HTTP requests:
[triggers]crons = ["0 * * * *"] # Every hourexport default { // HTTP handler (optional) async fetch(request: Request, env: Env) { return new Response('OK'); },
// Cron handler async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) { // This runs on schedule await checkForUpdates(env); },};Common Scheduled Tasks
Section titled “Common Scheduled Tasks”Daily Site Health Check
Section titled “Daily Site Health Check”> Create a Worker cron that runs daily and:> 1. Checks that our main site returns 200> 2. Checks response time> 3. Stores the result in D1> 4. Sends a Slack notification if anything is wrongWeekly Content Report
Section titled “Weekly Content Report”> Create a cron Worker that every Monday:> 1. Queries D1 for content published last week> 2. Generates engagement statistics> 3. Creates a summary report> 4. Stores it in R2Data Cleanup
Section titled “Data Cleanup”> Create a cron that runs nightly and:> 1. Deletes expired sessions from KV> 2. Removes orphaned files from R2> 3. Archives old records in D1Cron Schedule Syntax
Section titled “Cron Schedule Syntax”┌─── minute (0-59)│ ┌─── hour (0-23)│ │ ┌─── day of month (1-31)│ │ │ ┌─── month (1-12)│ │ │ │ ┌─── day of week (0-6, Sun=0)│ │ │ │ │* * * * *Common patterns:
*/5 * * * *— every 5 minutes0 * * * *— every hour0 0 * * *— daily at midnight0 0 * * 1— every Monday0 0 1 * *— first of every month
Building with Claude
Section titled “Building with Claude”Ask Claude to create the complete cron Worker:
> Create a Cloudflare Worker with a cron trigger that runs every 6 hours.> It should:> 1. Fetch our WordPress site's latest posts via REST API> 2. Compare with what's stored in D1> 3. If there are new posts, store them in D1> 4. Log a summary of new posts found>> Include the wrangler.toml configuration.Claude will create the Worker code, types, and Wrangler config — ready to deploy.
Exercise
Section titled “Exercise”- Think of a recurring task in your workflow
- Ask Claude to build a cron Worker for it
- Test locally with
wrangler dev - Deploy and monitor with
wrangler tail