-
Notifications
You must be signed in to change notification settings - Fork 708
developer-guide: add a TTL doc (#22464) #22471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ti-chi-bot
merged 2 commits into
pingcap:release-8.5
from
ti-chi-bot:cherry-pick-22464-to-release-8.5
Feb 14, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| --- | ||
| title: Periodically Delete Data Using TTL (Time to Live) | ||
| summary: Learn how to use the TTL feature of TiDB to automatically and periodically delete expired data. | ||
| --- | ||
|
|
||
| # Periodically Delete Data Using TTL (Time to Live) | ||
|
|
||
| In application development, some data is only valuable for a limited period of time. For example, verification codes typically need to be retained for only a few minutes, short links might be valid only during a specific campaign, and access logs or intermediate computation results are often kept for just a few months. | ||
|
|
||
| TiDB provides the [TTL (Time to Live)](/time-to-live.md) feature, which helps you manage the lifetime of TiDB data at the row level. With TTL, you can **automatically and periodically** remove expired data without writing complex scheduled cleanup scripts. | ||
|
|
||
| ## Use cases | ||
|
|
||
| TTL is designed for scenarios where data no longer has business value after a certain period of time. Typical use cases include the following: | ||
|
|
||
| - Periodically deleting verification codes and short URL records | ||
| - Periodically cleaning up outdated historical orders | ||
| - Automatically removing intermediate computation results | ||
|
|
||
| > **Note:** | ||
| > | ||
| > TTL jobs run periodically in the background. Therefore, expired data is not guaranteed to be deleted immediately after it reaches its expiration time. | ||
|
|
||
| ## Quick start | ||
|
|
||
| You can configure the TTL attribute when creating a table, or add it to an existing table. The following sections provide basic examples of how to use TTL to periodically delete expired data. For complete examples, usage restrictions, and compatibility details with other tools or features, see [TTL (Time to Live)](/time-to-live.md). | ||
|
|
||
| ### Create a table with TTL | ||
|
|
||
| To create a table named `app_messages` for storing instant messages and automatically delete messages three months after their creation, execute the following statement: | ||
|
|
||
| ```sql | ||
| CREATE TABLE app_messages ( | ||
| id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, | ||
| sender_id INT, | ||
| msg_content TEXT, | ||
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP | ||
| ) TTL = `created_at` + INTERVAL 3 MONTH; | ||
| ``` | ||
|
|
||
| In this example, `TTL = ...` defines the expiration policy. The `created_at` column records the creation time of each row, and `INTERVAL 3 MONTH` specifies that each row is retained for a maximum of three months. | ||
|
|
||
| ### Configure the TTL attribute for an existing table | ||
|
|
||
| If you already have a table named `app_logs` and want to enable automatic cleanup (for example, retaining only one month of data), execute the following statement: | ||
|
|
||
| ```sql | ||
| ALTER TABLE app_logs TTL = `created_at` + INTERVAL 1 MONTH; | ||
| ``` | ||
|
|
||
| ### Modify the TTL period | ||
|
|
||
| To modify the retention period for the `app_logs` table, execute the following statement: | ||
|
|
||
| ```sql | ||
| ALTER TABLE app_logs TTL = `created_at` + INTERVAL 6 MONTH; | ||
| ``` | ||
|
|
||
| ### Disable TTL | ||
|
|
||
| To disable TTL for the `app_logs` table, execute the following statement: | ||
|
|
||
| ```sql | ||
| ALTER TABLE app_logs TTL_ENABLE = 'OFF'; | ||
| ``` | ||
|
|
||
| ## See also | ||
|
|
||
| - [TTL (Time to Live)](/time-to-live.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.