# ALTER TABLE SET TTL

ALTER TABLE SET TTL SQL keyword reference documentation.

Sets the time-to-live (TTL) period on a table.

:::caution

**QuestDB Enterprise: TTL is deprecated.** Enterprise rejects any non-zero
`SET TTL` with
`TTL settings are deprecated, please, create a storage policy instead`. Use
[`ALTER TABLE SET STORAGE POLICY`](/docs/query/sql/alter-table-set-storage-policy/)
instead. `SET TTL 0` is still accepted, for clearing a pre-existing TTL before
attaching a storage policy. See [Storage Policy](/docs/concepts/storage-policy/)
for the Enterprise replacement.

:::

Refer to the [section on TTL](/docs/concepts/ttl/) for a conceptual overview.

## Syntax

```questdb-sql
ALTER TABLE tableName SET TTL n { HOUR[S] | DAY[S] | WEEK[S] | MONTH[S] | YEAR[S] };
```

## Description

To store and analyze only recent data, configure a time-to-live (TTL) period on
a table using the `ALTER TABLE SET TTL` command.

Follow the `TTL` keyword with a number and a time unit, one of:

- `HOURS`
- `DAYS`
- `WEEKS`
- `MONTHS`
- `YEARS`

TTL units fall into two categories:

1. Fixed time periods:
   - `HOURS`
   - `DAYS`
   - `WEEKS`
2. Calendar-based periods:
   - `MONTHS`
   - `YEARS`

Fixed-time periods are always exact durations: `1 WEEK` is always 7 days.

Calendar-based periods may vary in length: `1 MONTH` from January 15th goes to
February 15th and could be between 28 and 31 days.

QuestDB accepts both singular and plural forms:

- `HOUR` or `HOURS`
- `DAY` or `DAYS`
- `WEEK` or `WEEKS`
- `MONTH` or `MONTHS`
- `YEAR` or `YEARS`

:::note

QuestDB drops data that exceeded its TTL only a whole partition at a time. For
this reason, the TTL period must be a whole number multiple of the table's
partition size.

For example:

- If a table is partitioned by `DAY`, the TTL must be a whole number of days
  (`24 HOURS`, `2 DAYS` and `3 MONTHS` are all accepted)
- If a table is partitioned by `MONTH`, the TTL must be in months or years.
  QuestDB won't accept the `HOUR`, `DAY`, or `WEEK` units

Refer to the [section on TTL in Concepts](/docs/concepts/ttl/) for detailed
information on the behavior of this feature.

:::

### Shorthand notation

QuestDB also supports a shorthand notation that combines the number and unit
into a single token, such as `3h` for 3 hours or `2M` for 2 months:

```questdb-sql
ALTER TABLE tableName SET TTL n{h|d|w|M|y};
```

## Examples

Set the TTL to 3 weeks:

```questdb-sql
ALTER TABLE weather SET TTL 3 WEEKS;
```

Set the TTL to 12 hours, using the shorthand syntax for the time unit:

```questdb-sql
ALTER TABLE weather SET TTL 12h;
```

Disable TTL:

```questdb-sql
ALTER TABLE weather SET TTL 0h;
```
