How to use Time to Live (TTL) - Amazon Keyspaces (for Apache Cassandra)

How to use Time to Live (TTL)

You can use the Amazon Keyspaces (for Apache Cassandra) console or CQL to enable, update, and disable Time to Live settings.

To create a new table with default Time to Live (TTL) settings enabled (console)

Follow these steps to create a new table with Time to Live settings enabled using the Amazon Keyspaces console.

  1. Sign in to the AWS Management Console, and open the Amazon Keyspaces console at https://console.aws.amazon.com/keyspaces/home.

  2. In the navigation pane, choose Tables, and then choose Create table.

  3. On the Create table page in the Table details section, select a keyspace and provide a name for the new table.

  4. In the Schema section, create the schema for your table.

  5. In the Table settings section, choose Customize settings.

  6. Continue to Time to Live (TTL).

    In this step, you select the default TTL settings for the table.

    For the Default TTL period, enter the expiration time and choose the unit of time you entered, for example seconds, days, or years. Amazon Keyspaces will store the value in seconds.

  7. Choose Create table. Your table is created with the specified default TTL value.

Note

You can overwrite the table's default TTL setting for specific rows or columns by using the data manipulation language (DML) in the CQL editor.

To update default Time to Live (TTL) settings on existing tables (console)

Follow these steps to update Time to Live settings for existing tables using the Amazon Keyspaces console.

  1. Sign in to the AWS Management Console, and open the Amazon Keyspaces console at https://console.aws.amazon.com/keyspaces/home.

  2. Choose the table that you want to update, and then choose the Additional settings tab.

  3. Continue to Time to Live (TTL) and choose Edit.

  4. For the Default TTL period, enter the expiration time and choose the unit of time you entered, for example seconds, days, or years. Amazon Keyspaces will store the value in seconds. This doesn't change the TTL value of existing rows.

  5. When the TTL settings are defined, choose Save changes.

To disable default Time to Live (TTL) settings on existing tables (console)

Follow these steps to disable Time to Live settings for existing tables using the Amazon Keyspaces AWS Management Console.

  1. Sign in to the AWS Management Console, and open the Amazon Keyspaces console at https://console.aws.amazon.com/keyspaces/home.

  2. Choose the table that you want to update, and then choose the Additional settings tab.

  3. Continue to Time to Live (TTL) and choose Edit.

  4. Select Default TTL Period and set the value to zero. This disables TTL for the table by default for future data. It doesn't change the TTL value for existing rows.

  5. When the TTL settings are defined, choose Save changes.

To create a new table with default Time to Live (TTL) settings enabled using CQL

Enable TTL when you're creating a new table with the default TTL value set to 3,024,000 seconds, which represents 35 days.

CREATE TABLE my_table ( userid uuid, time timeuuid, subject text, body text, user inet, PRIMARY KEY (userid, time) ) WITH default_time_to_live = 3024000;

To confirm the TTL settings for the new table, use the cqlsh describe statement as shown in the following example. The output shows the default TTL setting for the table as default_time_to_live.

describe my_table;

To use ALTER TABLE to edit default Time to Live (TTL) settings using CQL

Update the TTL settings of the existing table to 2,592,000 seconds, which represents 30 days.

ALTER TABLE my_table WITH default_time_to_live = 2592000;

To confirm the TTL settings for the updated table, use the cqlsh describe statement as shown in the following example. The output shows the default TTL setting for the table as default_time_to_live.

describe my_table;

How to enable Time to Live (TTL) on new tables using custom properties

To enable Time to Live custom settings that can be applied to rows and columns without enabling TTL default settings for the entire table, you can use the following CQL statement.

CREATE TABLE my_keyspace.my_table (id int primary key) WITH CUSTOM_PROPERTIES={'ttl':{'status': 'enabled'}};

After ttl is enabled, you can't disable it for the table.

How to enable Time to Live (TTL) on existing tables using custom properties

To enable Time to Live custom settings that can be applied to rows and columns without enabling TTL default settings for the entire table, you can use the following CQL statement.

ALTER TABLE my_table WITH CUSTOM_PROPERTIES={'ttl':{'status': 'enabled'}};

After ttl is enabled, you can't disable it for the table.

To use INSERT to edit custom Time to Live (TTL) settings using CQL

The following CQL statement inserts a row of data into the table and changes the default TTL setting to 259,200 seconds (which is equivalent to 3 days).

INSERT INTO my_table (userid, time, subject, body, user) VALUES (B79CB3BA-745E-5D9A-8903-4A02327A7E09, 96a29100-5e25-11ec-90d7-b5d91eceda0a, 'Message', 'Hello','205.212.123.123') USING TTL 259200;

To confirm the TTL settings for the inserted row, use the following statement.

SELECT TTL (subject) from my_table;

To use UPDATE to edit custom Time to Live (TTL) settings using CQL

To change the TTL settings of the 'subject' column inserted earlier from 259,200 seconds (3 days) to 86,400 seconds (one day), use the following statement.

UPDATE my_table USING TTL 86400 set subject = 'Updated Message' WHERE userid = B79CB3BA-745E-5D9A-8903-4A02327A7E09 and time = 96a29100-5e25-11ec-90d7-b5d91eceda0a;

You can run a simple select query to see the updated record before the expiration time.

SELECT * from my_table;

The query shows the following output.

userid | time | body | subject | user --------------------------------------+--------------------------------------+-------+-----------------+----------------- b79cb3ba-745e-5d9a-8903-4a02327a7e09 | 96a29100-5e25-11ec-90d7-b5d91eceda0a | Hello | Updated Message | 205.212.123.123 50554d6e-29bb-11e5-b345-feff819cdc9f | cf03fb21-59b5-11ec-b371-dff626ab9620 | Hello | Message | 205.212.123.123

To confirm that the expiration was successful, run the same query again after the configured expiration time.

SELECT * from my_table;

The query shows the following output after the 'subject' column has expired.

userid | time | body | subject | user --------------------------------------+--------------------------------------+-------+---------+----------------- b79cb3ba-745e-5d9a-8903-4a02327a7e09 | 96a29100-5e25-11ec-90d7-b5d91eceda0a | Hello | null | 205.212.123.123 50554d6e-29bb-11e5-b345-feff819cdc9f | cf03fb21-59b5-11ec-b371-dff626ab9620 | Hello | Message | 205.212.123.123