---
title: "Schedule jobs"
description: "Add your app's fixed jobs to crons.ts with Convex's cronJobs, schedule per-person jobs at runtime with the crons component, and see the maintenance the backend component runs on its own."
canonical_url: "https://nuxt-backend.dev/docs/build/data/jobs/scheduled"
---
# Schedule jobs

> Add your app's fixed jobs to crons.ts with Convex's cronJobs, schedule per-person jobs at runtime with the crons component, and see the maintenance the backend component runs on its own.

Version: nuxt-backend 0.6.x · Tab: Build · Kind: how-to · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

Send a weekly digest every Monday, refresh exchange rates every six hours, or remind each reader at the hour they picked. Fixed schedules go in `backend/crons.ts` with Convex's own `cronJobs()`. Schedules that are data, one per person or per row, go through the official [`@convex-dev/crons`](https://www.convex.dev/components/crons) component. The `backend` component's own maintenance needs nothing from you.

## Your own jobs

`backend/crons.ts` is Convex's file for jobs that are the same on every deployment. Create it with your first job:

```ts [backend/crons.ts]
import { cronJobs } from 'convex/server'
import { internal } from './_generated/api'

const crons = cronJobs()

crons.cron('send weekly digest', '15 8 * * 1', internal.digest.send, {})
crons.interval('refresh rates', { hours: 6 }, internal.rates.refresh, { currency: 'eur' })

export default crons
```

- **crons.cron(name, expression, fn, args)** runs on a cron expression, in UTC. Pick a minute other than `0`, so your job does not start with everyone else's.
- **crons.interval(name, { hours, minutes, seconds }, fn, args)** runs on an interval from the first deploy.
- **The function** is an internal one, referenced through `internal`, with the arguments its validator asks for.

A job with a lot to do works in batches: one transaction each, scheduling the next until nothing is left. For rows that pass an age, a [retention rule](https://nuxt-backend.dev/docs/build/data/retention) writes that loop for you. The Convex guide to [cron jobs](https://docs.convex.dev/scheduling/cron-jobs) has the rest.

## Jobs your code schedules

Some schedules are data: a digest at the hour each reader picked, a reminder that repeats until someone answers. The crons component keeps them by name, each with its own time zone. Install and mount it:

```bash
npm install @convex-dev/crons
```

<code-tree default-value="backend/digest.ts">

```ts [backend/convex.config.ts]
import crons from '@convex-dev/crons/convex.config'

// …after the package's components
app.use(crons)
```

```ts [backend/digest.ts]
import { Crons } from '@convex-dev/crons'
import { v } from 'convex/values'
import { components, internal } from './_generated/api'
import { authed } from './functions'

const schedules = new Crons(components.crons)

export const setDigestHour = authed.mutation({
  args: { hour: v.number(), timeZone: v.string() },
  handler: async (ctx, { hour, timeZone }) => {
    const name = `digest:${ctx.user.id}`
    if (await schedules.get(ctx, { name })) await schedules.delete(ctx, { name })
    await schedules.register(
      ctx,
      { kind: 'cron', cronspec: `20 ${hour} * * *`, tz: timeZone },
      internal.digest.sendTo,
      { userId: ctx.user.id },
      name,
    )
  },
})
```

</code-tree>

A component's functions are never public on their own: wrap them in your own functions, behind a tier, as above. `register` takes a cron or an interval, `get` and `list` read what is scheduled, and `delete` stops a job. The component's [README](https://www.convex.dev/components/crons) lists every method.

## The maintenance the component runs

The `backend` component's own cron starts a pass every hour. Each task deletes what is past its window, a few hundred rows per transaction, and schedules its next batch until nothing is left. A pass fixes its cutoff when it starts, so it always ends. A batch that throws rolls back, ends its task's pass and logs `maintenance.task_failed`; the next pass tries again.

<table>
<thead>
  <tr>
    <th>
      Task
    </th>
    
    <th>
      Deletes
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        sessions
      </code>
    </td>
    
    <td>
      Sessions that expired
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        verifications
      </code>
    </td>
    
    <td>
      Expired sign-in codes and email-change tokens
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        rateLimits
      </code>
    </td>
    
    <td>
      Rate-limit counters that went idle, when an app turns that limiter on
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        aiRequests
      </code>
    </td>
    
    <td>
      The records of metered AI requests that settled or were released, and of one stuck as reserved for a week
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        invitations
      </code>
    </td>
    
    <td>
      Invitations nobody answered, well after they expired
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        memberTickets
      </code>
    </td>
    
    <td>
      A <a href="https://nuxt-backend.dev/docs/build/growth/launch/invites#let-members-bring-friends">
        member ticket
      </a>
      
      , 30 days after it ended
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        auditEvents
      </code>
    </td>
    
    <td>
      Activity rows past their year
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        accountExports
      </code>
    </td>
    
    <td>
      An account's data export a day after it was made, with its file
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        failedErasures
      </code>
    </td>
    
    <td>
      An <a href="https://nuxt-backend.dev/docs/build/auth/accounts/data">
        erasure
      </a>
      
       that stopped at a failing source and was not run again within 30 days
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        billingDeletions
      </code>
    </td>
    
    <td>
      A billing entity's deletion mark, 30 days after the erasure that set it
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        billingConsents
      </code>
    </td>
    
    <td>
      A renewing checkout's consent proof three years after the consent, or two days after a checkout that started nothing (<a href="https://nuxt-backend.dev/docs/ship/compliance/subscriptions">
        Follow subscription law
      </a>
      
      )
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        billingWithdrawals
      </code>
    </td>
    
    <td>
      An EU withdrawal's record, three years after it
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        billingSaveOffers
      </code>
    </td>
    
    <td>
      The cancel flow's save offers, two years after they were made
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        billingLifecycleMarks
      </code>
    </td>
    
    <td>
      What the billing lifecycle did once (an annual reminder, an abandoned checkout's record), two years on
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        secretOutbox
      </code>
    </td>
    
    <td>
      A secret email still waiting after its code expired
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        emailRecords
      </code>
    </td>
    
    <td>
      Email records with a final status, by the windows <a href="https://nuxt-backend.dev/docs/build/email/sending">
        <code>
          setupEmail({ retention })
        </code>
      </a>
      
       sets
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        storageOrphans
      </code>
    </td>
    
    <td>
      Uploads nobody claimed, a day after they arrived (<a href="https://nuxt-backend.dev/docs/build/data/files/cleanup">
        Keep storage clean
      </a>
      
      )
    </td>
  </tr>
</tbody>
</table>

`storageOrphans` arrives with FILES. The sandbox inbox's copies are gone: the dev inbox reads sent mail back from Resend instead of keeping copies.

DevTools shows each task's last pass and the rows it deleted on the [Overview tab](https://nuxt-backend.dev/docs/develop/devtools#overview). `npx convex run --component backend maintenance:status` prints the same. [Limits and retention](https://nuxt-backend.dev/docs/reference/backend/limits#windows-and-lifetimes) gives each window.

<note>

On a deployment that already holds data, the first pass deletes the backlog. It works through it a batch at a time, so it may take a few minutes, and it never deletes anything still in use.

</note>

## Related

- [Background work](https://nuxt-backend.dev/docs/build/data/jobs): which tool to pick.
- [Delete old data on a schedule](https://nuxt-backend.dev/docs/build/data/retention): `defineRetention`, started from `crons.ts`.
- [Run a durable workflow](https://nuxt-backend.dev/docs/build/data/jobs/workflows): work of several steps, with waits between them.
- Convex [cron jobs](https://docs.convex.dev/scheduling/cron-jobs) and the [crons component](https://www.convex.dev/components/crons).


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
