Expiring data with TTL example

Use the INSERT and UPDATE commands for setting the expiration time-to-live (TTL) for data in a column.

Both the INSERT and UPDATE commands support setting a time for data in a column to expire, including collections and user-defined types. Use CQL to set the expiration time-to-live (TTL).

Procedure

  • Use the INSERT command to set a calendar listing in the calendar table to expire in 200 seconds.
    INSERT INTO cycling.calendar (
      race_id, race_name, race_start_date, race_end_date
    ) VALUES (
      200, 'placeholder', '2015-05-27', '2015-05-27'
    )
    USING TTL 200;
  • Extend the expiration period to 300 seconds by using the UPDATE command with the USING TTL keywords. The example also updates the race name.
    UPDATE cycling.calendar 
    USING TTL 300 
    SET race_name = 'Tour de France - Stage 12' 
    WHERE race_id = 200 
      AND race_start_date = '2015-05-27' 
      AND race_end_date = '2015-05-27';
  • Delete a column's existing TTL by setting its value to zero.
    UPDATE cycling.calendar 
    USING TTL 0
    SET race_name = 'Tour de France - Stage 12' 
    WHERE race_id = 200 
      AND race_start_date = '2015-05-27' 
      AND race_end_date = '2015-05-27';

    You can set a default TTL for an entire table by setting the table's default_time_to_live property. Setting TTL on a column using the INSERT or UPDATE command overrides the table TTL.