# Scheduled functions

Source: https://cubibase.com/docs/functions/schedules

Also available: [Markdown version](https://cubibase.com/docs/functions/schedules.md) · [llms.txt](https://cubibase.com/llms.txt) · [full site content](https://cubibase.com/llms-full.txt)

Run a function on a cron schedule without operating a scheduler. Each schedule trigger owns exactly one cron entry; runs pass through the same quotas, logging and retries as every other trigger.

## Configure a schedule

In the function editor open Triggers → Add trigger → Scheduled. Provide a standard 5-field cron expression and a timezone (UTC by default, any IANA name accepted).

**Trigger configuration · 03:00 UTC daily**

```json
{
  "kind": "schedule",
  "config": {
    "cron": "0 3 * * *",
    "timezone": "UTC"
  }
}
```

- The trigger row shows the last run (with its outcome) and the next scheduled run.
- Editing a schedule replaces it in place — deployments never create duplicate active schedules.
- Disabling the trigger stops future runs immediately; deleting it removes the schedule.
- The number of enabled schedules per project is plan-limited.

## The tick payload and missed runs

The handler receives `request.event` of type `schedule.tick` with the cron expression, the scheduled time and the timezone. If the platform was down at the scheduled moment, missed runs collapse into a single catch-up run rather than a burst.

**Nightly cleanup**

```ts
export default async function handler(request: any, ctx: any) {
  // request.event.type is 'schedule.tick'; the cron cursor already advanced.
  // Equality filters only in ctx.db — for time ranges, find ids with
  // read-only SQL, then delete by primary key.
  const stale = await ctx.db.query(
    "SELECT id FROM public.sessions WHERE expires_at < now() - interval '30 days' LIMIT 500",
  );
  let deleted = 0;
  for (const row of stale.rows ?? []) {
    await ctx.db.from('sessions').delete({ id: row.id });
    deleted += 1;
  }
  console.log('Cleanup deleted', deleted, 'rows');
  return { deleted };
}
```

> **note**: Scheduled invocations are metered exactly like HTTP calls. An exhausted monthly allowance rejects the run (it is retried, then dead-lettered) — a scheduler cannot bypass your quota.

## Additional resources

- [Templates](https://cubibase.com/docs/functions/templates) Scheduled Cleanup is a working nightly job.
- [Quotas](https://cubibase.com/docs/functions/quotas) Schedule counts and metering.
