Configuring Time to Live (TTL)

This section contains recipes for configuration of table's TTL with YDB CLI.

Enabling TTL for an existing table

In the example below, the items of the mytable table will be deleted an hour after the time set in the created_at column:

$ ydb -e <endpoint> -d <database> table ttl set --column created_at --expire-after 3600 mytable

The example below shows how to use the modified_at column with a numeric type (Uint32) as a TTL column. The column value is interpreted as the number of seconds since the Unix epoch:

$ ydb -e <endpoint> -d <database> table ttl set --column modified_at --expire-after 3600 --unit seconds mytable

Enabling data eviction to S3-compatible external storage

Warning

Supported only for column-oriented tables. Support for row-oriented tables is currently under development.

To enable data eviction, an external data source object that describes a connection to the external storage is needed. Refer to YQL recipe for examples of creating an external data source.

The example below shows how to enable data eviction by executing a YQL-query from YDB CLI. Rows of the table mytable will be moved to the bucket described in the external data source /Root/s3_cold_data one hour after the time recorded in the column created_at and will be deleted after 24 hours.

$ ydb -e <endpoint> -d <database> table query execute -q '
    ALTER TABLE `mytable` SET (
        TTL =
            Interval("PT1H") TO EXTERNAL DATA SOURCE `/Root/s3_cold_data`,
            Interval("PT24H") DELETE
        ON modified_at AS SECONDS
    );
'

Disabling TTL

$ ydb -e <endpoint> -d <database> table ttl reset mytable

Getting TTL settings

The current TTL settings can be obtained from the table description:

$ ydb -e <endpoint> -d <database> scheme describe mytable
Previous