Cron Triggers & Scheduled Workers
Run Cloudflare Workers on a schedule with Cron Triggers to automate recurring background tasks at the edge.
Cron Triggers & Scheduled Workers is a free Edge Computing with Cloudflare Workers & Deno lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Edge Computing with Cloudflare Workers & Deno learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Scheduled Workers?
Beyond responding to HTTP requests, Workers can run on a schedule using Cron Triggers.
This is perfect for recurring jobs such as:
- Cleaning up stale data in KV or D1
- Sending daily report emails
- Refreshing cached API responses
- Aggregating analytics
No always-on server required, the Worker simply wakes up, runs, and goes back to sleep.
The scheduled() Handler
A scheduled Worker exports a scheduled() handler instead of (or alongside) fetch().
Cloudflare invokes it automatically at the times you define in cron expressions.
export default {
async scheduled(event, env, ctx) {
console.log('Triggered at: ' + event.scheduledTime);
}
};Configuring Cron in wrangler.toml
Cron schedules live in your wrangler.toml under the [triggers] section.
You can declare multiple cron expressions, each one fires the same scheduled() handler.
[triggers]
crons = ["0 * * * *", "*/15 * * * *"]Cron Expression Syntax
Cloudflare cron expressions have five fields: minute, hour, day-of-month, month, day-of-week.
0 0 * * *means midnight UTC every day*/5 * * * *means every 5 minutes0 9 * * 1means 09:00 UTC every Monday
All times are in UTC.
Distinguishing Multiple Crons
When you register several cron schedules, use event.cron to tell which one fired.
This lets one Worker handle many jobs.
export default {
async scheduled(event, env, ctx) {
if (event.cron === '0 0 * * *') {
await runDailyCleanup(env);
} else if (event.cron === '*/15 * * * *') {
await refreshCache(env);
}
}
};Using waitUntil for Long Tasks
Just like in fetch(), the ctx.waitUntil() method keeps the Worker alive until a promise resolves.
Use it to ensure background async work completes before the invocation ends.
async scheduled(event, env, ctx) {
ctx.waitUntil(syncRemoteData(env));
}Accessing Bindings in scheduled()
The env argument exposes all your bindings: KV, D1, R2, Queues, secrets.
A scheduled cleanup might delete expired KV keys.
async scheduled(event, env, ctx) {
const list = await env.MY_KV.list({ prefix: 'session:' });
for (const key of list.keys) {
await env.MY_KV.delete(key.name);
}
}Testing Crons Locally
Wrangler lets you test scheduled handlers without waiting for the real schedule.
Run wrangler dev and trigger via the test endpoint, or use the CLI flag.
wrangler dev --test-scheduled
# then visit:
# http://localhost:8787/__scheduled?cron=0+*+*+*+*Combining fetch() and scheduled()
A single Worker can export both handlers.
This is handy when the same logic serves HTTP requests and runs on a timer.
export default {
async fetch(request, env) {
return new Response('Hello from fetch');
},
async scheduled(event, env, ctx) {
console.log('Hello from cron');
}
};Monitoring Scheduled Runs
Cron invocations appear in the Cloudflare dashboard and in wrangler tail logs.
Watch for failures, a thrown error means the job did not complete, and there is no automatic retry for cron triggers.
wrangler tail --format prettyLimits & Best Practices
Keep scheduled jobs efficient:
- Stay within CPU time limits, offload heavy work to Queues
- Make jobs idempotent so reruns are safe
- Avoid sub-minute schedules unless necessary
- Log results for observability
Quick Check
Which property tells you which cron schedule triggered the handler?
Recap
You learned how to run Workers on a schedule:
- Export a
scheduled()handler - Define schedules in
[triggers] crons - Use five-field UTC cron expressions
- Route with
event.cronand keep work alive withctx.waitUntil() - Test with
--test-scheduledand monitor withwrangler tail
Cron Triggers turn Workers into reliable edge-native background job runners.
Frequently asked questions
Is the “Cron Triggers & Scheduled Workers” lesson free?
Yes — the full text of “Cron Triggers & Scheduled Workers” is free to read here on the web, and the Edge Computing with Cloudflare Workers & Deno course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Edge Computing with Cloudflare Workers & Deno course, upgrade to CoddyKit PRO.
What will I learn in “Cron Triggers & Scheduled Workers”?
Run Cloudflare Workers on a schedule with Cron Triggers to automate recurring background tasks at the edge. You practise Edge Computing with Cloudflare Workers & Deno with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Edge Computing with Cloudflare Workers & Deno?
No prior experience is required. Edge Computing with Cloudflare Workers & Deno on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Cron Triggers & Scheduled Workers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Edge Computing with Cloudflare Workers & Deno lesson?
Yes. Every Edge Computing with Cloudflare Workers & Deno lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- WebSockets & Real-time
- Queues & Asynchronous Tasks
- Service Bindings & Integrations
- Cron Triggers & Scheduled Workers