How to set up cloud cron jobs with Netlify scheduled functions

How to set up cloud cron jobs with Netlify scheduled functions

Perttu Lähteenlahti
4 min read
scheduled-functionscron-jobsserverlesstypescript
Share:

Why you might need cron jobs on Netlify

At some point, most projects need background work. Maybe you want to clean up data, sync something once a day, or trigger a redeploy at a specific time.

On traditional servers, you would use cron jobs. But when you are building on Netlify, you probably do not want to manage servers at all.

This is where Netlify Scheduled Functions come in. They let you run serverless functions on a schedule. You can think of them as cloud based cron jobs, fully managed by Netlify.

What are Netlify Scheduled Functions

Netlify Scheduled Functions are regular Netlify Functions that run automatically based on a schedule you define.

They:

  • Run in the background
  • Do not need user interaction
  • Can run hourly, daily, weekly, or using custom cron expressions
  • Are easy to deploy with your existing Netlify project

Once set up, Netlify takes care of running them on time.

Step 1: Create a Netlify function

Start by creating a function inside your repository.

Create this file:

netlify/functions/test.ts

Here is a simple TypeScript Netlify Scheduled Function using ES modules:

import { Handler } from "@netlify/functions"

export const handler: Handler = async () => {
  // Do your scheduled work here
  console.log("Scheduled function ran")

  return {
    statusCode: 200
  }
}

At this point, the function exists but it does not run on a schedule yet.

Step 2: Add a schedule using netlify.toml

The most common way to schedule a Netlify function is through netlify.toml.

If you do not have this file yet, create it at the root of your project.

Add the following:

[functions."test"]
schedule = "@hourly"

This tells Netlify to run the test function every hour, at minute zero.

After you deploy, Netlify will automatically start running the function on this schedule.

Common schedule options on Netlify

Netlify supports several built in schedules:

  • @hourly runs every hour
  • @daily runs every day at 00:00
  • @weekly runs every Sunday at 00:00
  • @monthly runs once a month
  • @yearly runs once a year

You can also use full cron expressions like:

5 4 * * *

That example runs every day at 04:05.

If you are unsure about cron syntax, tools like crontab.guru are very helpful.

Alternative: define the schedule inside the function

Instead of using netlify.toml, you can define the schedule directly in the function file.

This approach keeps everything in one place.

Here is the same function with an inline schedule:

import { Handler, schedule } from "@netlify/functions"

const task: Handler = async () => {
  console.log("Running scheduled task")

  return {
    statusCode: 200
  }
}

export const handler = schedule("@hourly", task)

This does the same thing as the netlify.toml configuration, but some developers prefer it because it is more explicit in code.

Running the function manually

Even though the function runs on a schedule, you can still invoke it manually.

From the Netlify CLI, you can run:

netlify functions:invoke test

This is very useful for testing your logic before waiting for the schedule to trigger.

Real world use cases for Netlify Scheduled Functions

Netlify Scheduled Functions are useful for many tasks, such as:

  • Triggering automatic redeploys
  • Publishing scheduled blog posts
  • Cleaning up old data
  • Syncing content from external APIs
  • Sending periodic reports or pings

One common pattern is using the Fetch API inside a scheduled function to call a Netlify deploy hook, which forces a rebuild at a specific time.

This works great for static sites that publish content based on dates.

Conclusion

Netlify Scheduled Functions give you an easy way to run background jobs without managing infrastructure.

To summarize:

  • Create a Netlify function in netlify/functions
  • Add a schedule using netlify.toml or the schedule() helper
  • Use simple schedules or full cron expressions
  • Let Netlify handle the execution

If you need cloud cron jobs and you are already using Netlify, Scheduled Functions are one of the cleanest and simplest solutions available.

Manage Netlify on the go

Download Netli.fyi and monitor your sites, check deploys, and manage your projects from anywhere.

Related articles