Running scheduled tasks like database cleanups, email reminders, or periodic data fetching traditionally requires a server or a cron scheduler — which usually costs money or infrastructure. But using Cloudflare Workers and Durable Objects, you can schedule cron-like tasks completely serverlessly.

Why Use Cloudflare Workers for Cron?

  • Free-tier available
  • No infrastructure to manage
  • Edge-deployed functions
  • Easy integration with APIs, storage, or databases

Step 1: Install Wrangler

npm install -g wrangler

Initialize your project:

wrangler init scheduled-job --type=javascript
cd scheduled-job

Step 2: Enable Scheduled Events in wrangler.toml

[triggers]
crons = ["0 */1 * * *"]  # every hour on the hour

This follows the standard crontab syntax. You can schedule it every minute, hour, day, etc.

Step 3: Add the Scheduled Handler

export default {
  async scheduled(controller, env, ctx) {
    console.log("Cron job triggered!");

    // Example: Cleanup logic
    await fetch("https://your-api.com/cleanup", { method: "POST" });
  },

  async fetch(request, env, ctx) {
    return new Response("Worker is live!");
  }
}

This defines a worker that performs a task on schedule and responds normally to HTTP requests.

Step 4: Deploy

wrangler deploy

Your cron jobs are now running without a traditional server.

Step 5 (Optional): Use Durable Objects for Stateful Logic

If you need to keep state between cron executions (e.g., counters, timestamps), use Durable Objects:

export class CronState {
  constructor(state, env) {
    this.state = state;
  }

  async fetch(request) {
    let value = await this.state.storage.get("lastRun");
    return new Response("Last ran at: " + value);
  }

  async scheduled(controller, env, ctx) {
    const now = new Date().toISOString();
    await this.state.storage.put("lastRun", now);
  }
}

Configure Durable Object binding in wrangler.toml and attach it in your main Worker file.

Pros and Cons

✅ Pros

  • Zero-cost scheduled tasks with high reliability
  • No server or Docker containers to maintain
  • Integrated state via Durable Objects

⚠️ Cons

  • Max 3 scheduled events per Worker on the free tier
  • Limited to 1-minute granularity
  • More complex than traditional crontab for simple tasks

🚀 Alternatives

  • GitHub Actions: Can also run cron jobs on schedule for free
  • Render or Fly.io: Free cron job support with container-based deployment
  • Vercel/Netlify: Scheduled Functions available on higher tiers

Conclusion

Using Cloudflare Workers with Scheduled Events (and Durable Objects for state), you can execute serverless cron jobs that are fast, free, and reliable. This makes them ideal for low-maintenance production tasks or solo dev tools with no infrastructure.

If this guide helped you, support more like it at: buymeacoffee.com/hexshift