All Products
Search
Document Center

PolarDB:TTL Usage Guide

Last Updated:Feb 11, 2026

This topic describes the expired data cleanup feature of PolarDB for MySQL.

Version requirements

The database engine versions applicable to PolarDB MySQL Edition are as follows:

  • MySQL 8.0.1, with a minor version of 8.0.1.1.49.2 or later.

  • MySQL 8.0.2, with a minor version of 8.0.2.2.29.2 or later.

To view your kernel version, see the Kernel Version Guide.

Important notes

  • You cannot set the TTL property on temporary tables, such as local and global temporary tables.

  • Tables with the TTL property do not support features such as IMCI, Global Secondary Index (GSI), or partitioned tables.

  • A table with the TTL property cannot serve as the primary table in a foreign key constraint referenced by another table.

  • You cannot define triggers on a table with the TTL property.

  • Expired data is not guaranteed to be deleted immediately. The deletion time depends on the scheduling cycle of the background cleanup task.

  • When restoring data from a backup or a database dump, set loose_innodb_enable_ttl_purge to OFF to disable TTL-based cleanup. This prevents unintended deletion because all restored data may already be expired.

  • The column specified for TTL must be of the TIMESTAMP type.

  • Data cleaned up by TTL does not generate binary logging (binlog) records. When using binlog to synchronize data, if a table in the source database has TTL enabled, key conflicts may occur when synchronizing to the secondary database.

Syntax

You can configure TTL for a table using either the CREATE TABLE or ALTER TABLE statement.

Create a table with TTL

You can use one of the following methods to create a table with TTL:

Create a table named t1 and specify created_at as the TTL timestamp column to represent the data's creation time. Set the time-to-live (TTL) value for created_at to control data deletion.

  • Set the maximum TTL for created_at to 100 seconds.

    CREATE TABLE `t1` (
      `a` INT PRIMARY KEY,
      `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      KEY idx_created_at (`created_at`)
    )ENGINE=InnoDB  TTL='created_at@100';
    Note

    The TTL='created_at@100' setting specifies that rows in the table expire after 100 seconds. Expired data is deleted later during the cleanup process.

  • Set the maximum TTL for created_at to 3 hours.

    CREATE TABLE `t1` (
      `a` INT PRIMARY KEY,
      `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      KEY idx_created_at (`created_at`)
    )ENGINE=InnoDB  TTL='created_at' + INTERVAL 3 HOUR;
    Note
    • The TTL ='created_at'+ INTERVAL 3 HOUR setting configures a 3-hour TTL. Data older than 3 hours is automatically deleted.

    • This TTL (time-to-live) syntax supports multiple time units: YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, and SECOND. Choose the unit that best fits your data management needs.

Modify a table's TTL

You can use one of the following methods to modify the TTL property of a table:

Modify the TTL for the t1 table by adjusting the time-to-live (TTL) value for the created_at column.

  • Set the maximum TTL for created_at to 10,000 seconds.

    ALTER TABLE `t1` TTL='created_at@10000';
    Note

    Setting TTL to 10,000 seconds on the created_at column ensures data is deleted 10,000 seconds after its creation.

  • Set the maximum TTL for created_at to 3 days.

    ALTER TABLE `t1` TTL='created_at' + INTERVAL 3 DAY;
    Note

    Adding 3 days to the created_at timestamp sets the expiration time to 3 days. Data older than 3 days is automatically deleted.

Remove TTL from a table

ALTER TABLE t1 TTL = '';

Query a table's TTL setting

SHOW CREATE TABLE `t1` FULL;

CREATE TABLE `t1` (  
`a` INT PRIMARY KEY,  
`created_at` TIMESTAMP DEFAULT 
CURRENT_TIMESTAMP,  
 KEY idx_created_at (`created_at`)
)ENGINE=InnoDB  TTL='created_at@259200';
Note

You can run this statement to check whether a table has a TTL setting.

Parameter description

The following global parameters control TTL-based cleanup of expired data:

Note

PolarDB lets you modify the following global parameters in the console to control the cleanup of expired TTL data.

Parameter Name

Description

loose_innodb_enable_ttl_purge

Enables or disables TTL-based cleanup of expired data.

Valid values:

  • ON: Enables TTL-based cleanup.

  • OFF (default): Disables TTL-based cleanup.

Note

You must manually enable TTL cleanup in the console.

loose_innodb_ttl_min_interval

Minimum allowed TTL interval. Default value: 100. Unit: seconds.

loose_innodb_ttl_purge_thread

Number of threads used to clean up expired TTL data. Default value: 4. After you change this parameter, restart the innodb_enable_ttl_purge parameter for the change to take effect.

loose_innodb_ttl_cluster_index_purge_batch_size

If the TTL column has no index, the cleanup task scans the primary key. Default number of rows scanned per batch: 10,000.

loose_innodb_ttl_index_purge_batch_size

If the TTL column has an index, the cleanup task scans that index. Default number of rows scanned per batch: 500.

loose_innodb_ttl_purge_start_hour

Start hour for TTL cleanup tasks. Default value: 0. Valid values: 0 to 23. Must not exceed innodb_ttl_purge_end_hour.

loose_innodb_ttl_purge_end_hour

End hour for TTL cleanup tasks. Default value: 0. Valid values: 0 to 23. Must not be less than innodb_ttl_purge_start_hour.

loose_innodb_ttl_finished_job_expired_days

Retention period for records in the mysql.ttl_job_history system table. Default value: 90. Valid values: 1 to 365. Unit: days.

Monitor TTL

The system collects runtime metrics for TTL at regular intervals. It provides the mysql.ttl_job_history system table to display details about TTL cleanup tasks. The table columns are described below:

Column

Description

job_id

ID of the TTL cleanup job. Usually a millisecond-level timestamp.

table_name

Name of the table targeted by this TTL cleanup job.

state

Status of the TTL job: suspended, running, or completed.

start_time

Start time of the job.

finished_time

Completion time of the job.

expire_time

Expiration time of the data cleaned by this TTL job.

scan_cost

Time spent scanning this batch.

purge_cost

Time spent cleaning up this batch.

purge_rows

Number of rows cleaned up by this TTL job.