How data retention works
provides two methods for managing data retention: automated policies that run on a schedule, and manual operations for one-time cleanup. Both methods work by dropping s—entire files from disk—rather than deleting individual rows, making the process fast and efficient. Data retention works at the level, not row-by-row. only drops s where all the data falls within your specified time range. For example, if you have three s containing data from more than 36 hours ago, between 12-36 hours ago, and from the last 12 hours, a 24-hour retention policy only drops the oldest . The middle is retained because it contains some data newer than 24 hours—no individual rows are deleted from that . This chunk-based approach is faster than row-by-row deletion with ‘sDELETE command because
it deletes entire files from disk without requiring garbage collection and defragmentation.
Prerequisites
To follow the steps on this page:- Create a target with Real-time analytics enabled. You need your connection details. This procedure also works for .
Add a retention policy
Automatically drop data once its time value ages past a certain interval. When you create a data retention policy, automatically schedules a background job to drop old s.-
Choose your hypertable and retention interval
Decide which needs retention and how long to keep data before dropping it. For
example, to retain data for 24 hours on a named
conditions:The retention policy schedules a background job that periodically checks for s older than 24 hours and drops them. -
Verify the policy was created
Query the jobs view to see your new retention policy:
A data retention policy only allows you to drop s based on how far they are in the past. To
drop s based on how far they are in the future, manually drop chunks.
Data retention with continuous aggregates
Downsample your data by combining a data retention policy with continuous aggregates. If you configure your refresh policies correctly, you can delete old data from a without deleting it from any continuous aggregates. This lets you save on raw data storage while keeping summarized data for historical analysis.Example: Setting compatible retention and refresh policies
Consider aconditions that stores device temperatures with a daily continuous
aggregate:
conditions_summary_daily aggregate that stores daily temperature per device. The
aggregate refreshes every day, updating with any data changes from 7 days ago to 1 day ago.
Don’t set a 24-hour retention policy on the conditions . If you do, s
older than 1 day are dropped. When the aggregate refreshes, it sees that data was deleted and also
deletes the aggregate data. You end up with no data in the conditions_summary_daily table.
Instead, set a longer retention policy—for example, 30 days:
Retention on continuous aggregates
You can also apply data retention on a continuous aggregate itself. For example, keep raw data for 30 days, daily aggregates for 600 days, and no data beyond that:Manually drop chunks
Drop s manually for one-time cleanup operations. Manual drops are useful for ad hoc data management that doesn’t fit an automated schedule.Dropping s manually is a one-time operation. To automatically drop s as they age, set
up a data retention policy.
Drop chunks older than a date
To drop s older than a certain date, use thedrop_chunks function. Provide
the name of the and a time interval:
Drop chunks between two dates
Drop s within a specific time range. For example, to drop s with data between 3 and 4 months old:Drop chunks in the future
Drop s in the future, for example, to correct data with incorrect timestamps. To drop all s more than 3 months in the future:Monitor retention jobs
To see your scheduled data retention jobs and their execution statistics, query thetimescaledb_information.jobs and timescaledb_information.job_stats views:
Remove a retention policy
Remove an existing data retention policy using theremove_retention_policy function. Pass it the name of the
: