Expiring data with TTL example

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.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com