> ## Documentation Index
> Fetch the complete documentation index at: https://twigz.arqqin.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Scheduled Tasks (Crons)

> Automated background jobs that run on recurring schedules to maintain data integrity and system health

## Overview

The TWIGZ backend uses Convex cron jobs to automate recurring maintenance tasks. These jobs run on fixed schedules and call internal functions to handle cleanup and data hygiene operations.

**Location:** `convex/crons.ts`

<Note>
  All cron jobs call **internal** functions that are not directly accessible from client applications. They run automatically on the Convex server and require no manual intervention.
</Note>

## Scheduled Jobs

<CardGroup cols={1}>
  <Card title="Cleanup Abandoned Carts" icon="cart-shopping">
    **Schedule**: Daily at 2:00 AM UTC (`0 2 * * *`)
    **Internal Function**: `internal.customers.cartUtils.cleanupAbandonedCarts`
    **Parameter**: `daysOld: 30`

    Removes shopping carts that have been inactive for more than 30 days. This prevents stale cart data from accumulating in the database and ensures storage remains efficient.
  </Card>

  <Card title="Cleanup Invalid Carts" icon="triangle-exclamation">
    **Schedule**: Weekly on Sunday at 3:00 AM UTC (`0 3 * * 0`)
    **Internal Function**: `internal.customers.cartUtils.cleanupInvalidCarts`
    **Parameters**: None

    Identifies and removes carts that are in an invalid state, such as carts referencing deleted products or stores that no longer exist.
  </Card>

  <Card title="Cleanup Stale Device Tokens" icon="bell-slash">
    **Schedule**: Weekly on Monday at 4:00 AM UTC (`0 4 * * 1`)
    **Internal Function**: `internal.shared.notifications.cleanupStaleDeviceTokens`
    **Parameter**: `daysInactive: 90`

    Deactivates push notification device tokens (FCM/APNS) that have not been updated in over 90 days. This reduces failed notification delivery attempts and keeps the token registry clean.
  </Card>
</CardGroup>

## Schedule Summary

| Job                         | Schedule    | Frequency            | Purpose                              |
| --------------------------- | ----------- | -------------------- | ------------------------------------ |
| Cleanup Abandoned Carts     | `0 2 * * *` | Daily at 2 AM        | Remove carts older than 30 days      |
| Cleanup Invalid Carts       | `0 3 * * 0` | Weekly (Sunday 3 AM) | Remove carts with invalid references |
| Cleanup Stale Device Tokens | `0 4 * * 1` | Weekly (Monday 4 AM) | Deactivate tokens inactive 90+ days  |

## How It Works

<Steps>
  <Step title="Convex Cron Engine">
    Convex evaluates cron expressions and triggers the associated internal function at the specified time.
  </Step>

  <Step title="Internal Function Execution">
    The target internal function runs within a Convex mutation or action context, querying and modifying the database as needed.
  </Step>

  <Step title="Automatic Retries">
    Convex handles retries for transient failures. If a cron job fails, it will be retried according to Convex's built-in retry policy.
  </Step>
</Steps>

## Related Functions

* [Cart Utilities](/api-reference/customers/cart-utils) -- Customer cart management and the underlying cleanup logic
* [Notifications](/api-reference/shared/notifications) -- Push notification system including device token management

<Tip>
  Cron job execution logs are available in the Convex dashboard under the **Functions** tab. Filter by the internal function name to see execution history and any errors.
</Tip>
