documentation/query/sql/alter-table-set-ttl.md
Sets the time-to-live (TTL) period on a table.
Refer to the section on TTL for a conceptual overview.
To store and analyze only recent data, configure a time-to-live (TTL) period on
a table using the ALTER TABLE SET TTL command.
Follow the TTL keyword with a number and a time unit, one of:
HOURSDAYSWEEKSMONTHSYEARSTTL units fall into two categories:
HOURSDAYSWEEKSMONTHSYEARSFixed-time periods are always exact durations: 1 WEEK is always 7 days.
Calendar-based periods may vary in length: 1 MONTH from January 15th goes to
February 15th and could be between 28 and 31 days.
QuestDB accepts both singular and plural forms:
HOUR or HOURSDAY or DAYSWEEK or WEEKSMONTH or MONTHSYEAR or YEARSIt also supports shorthand notation: 3h for 3 hours, 2M for 2 months.
:::note
QuestDB drops data that exceeded its TTL only a whole partition at a time. For this reason, the TTL period must be a whole number multiple of the table's partition size.
For example:
DAY, the TTL must be a whole number of days
(24 HOURS, 2 DAYS and 3 MONTHS are all accepted)MONTH, the TTL must be in months or years.
QuestDB won't accept the HOUR, DAY, or WEEK unitsRefer to the section on TTL in Concepts for detailed information on the behavior of this feature.
:::
Set the TTL to 3 weeks:
ALTER TABLE weather SET TTL 3 WEEKS;
Set the TTL to 12 hours, using the shorthand syntax for the time unit:
ALTER TABLE weather SET TTL 12h;