Manipulations with Table TTL

MODIFY TTL

You can change table TTL with a request of the following form:

  1. ALTER TABLE table_name MODIFY TTL ttl_expression;

REMOVE TTL

TTL-property can be removed from table with the following query:

  1. ALTER TABLE table_name REMOVE TTL

Example

Consider the table with table TTL:

  1. CREATE TABLE table_with_ttl
  2. (
  3. event_time DateTime,
  4. UserID UInt64,
  5. Comment String
  6. )
  7. ENGINE MergeTree()
  8. ORDER BY tuple()
  9. TTL event_time + INTERVAL 3 MONTH;
  10. SETTINGS min_bytes_for_wide_part = 0;
  11. INSERT INTO table_with_ttl VALUES (now(), 1, 'username1');
  12. INSERT INTO table_with_ttl VALUES (now() - INTERVAL 4 MONTH, 2, 'username2');

Run OPTIMIZE to force TTL cleanup:

  1. OPTIMIZE TABLE table_with_ttl FINAL;
  2. SELECT * FROM table_with_ttl FORMAT PrettyCompact;

Second row was deleted from table.

  1. ┌─────────event_time────┬──UserID─┬─────Comment──┐
  2. 2020-12-11 12:44:57 1 username1
  3. └───────────────────────┴─────────┴──────────────┘

Now remove table TTL with the following query:

  1. ALTER TABLE table_with_ttl REMOVE TTL;

Re-insert the deleted row and force the TTL cleanup again with OPTIMIZE:

  1. INSERT INTO table_with_ttl VALUES (now() - INTERVAL 4 MONTH, 2, 'username2');
  2. OPTIMIZE TABLE table_with_ttl FINAL;
  3. SELECT * FROM table_with_ttl FORMAT PrettyCompact;

The TTL is no longer there, so the second row is not deleted:

  1. ┌─────────event_time────┬──UserID─┬─────Comment──┐
  2. 2020-12-11 12:44:57 1 username1
  3. 2020-08-11 12:44:57 2 username2
  4. └───────────────────────┴─────────┴──────────────┘

See Also