Expiring data with TTL example

Use the INSERT and UPDATE commands for setting the expire time (TTL) for data in a column.

Both the INSERT and UPDATE commands support setting a time for data in a column to expire. Use CQL to set the expiration time (TTL).

Procedure

  • Use the INSERT command to set a calendar listing in the calendar table to expire in 86400 seconds (one day).
    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 86400;
  • Extend the expiration period to three days (259200 seconds) by using the UPDATE command with the USING TTL keyword. Also set the race name.
    UPDATE cycling.calendar USING TTL 259200 
      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.