All Products
Search
Document Center

PolarDB:TTL usage

Last Updated:Oct 21, 2026

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

Version restrictions

PolarDB for MySQL supports the following database engine versions:

  • MySQL 8.0.1 requires minor version 8.0.1.1.49.2 or later.

  • MySQL 8.0.2 requires minor version 8.0.2.2.29.2 or later.

To view the kernel version, see Engine versions.

Usage notes

  • You cannot configure time to live (TTL) properties on temporary tables, including local temporary tables and global temporary tables.

  • Tables with TTL properties do not support Global Secondary Indexes (GSIs).

  • A table with TTL properties cannot be referenced by other tables as a primary table in a foreign key constraint.

  • A table with TTL properties cannot contain triggers.

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

  • When you restore databases and tables or restore from backups, set loose_innodb_enable_ttl_purge to OFF to disable the cleanup of TTL-expired data. This is because all data might have expired after the restoration.

  • A column with TTL properties must be of the TIMESTAMP or DATETIME type.

  • Data cleaned up by TTL is not recorded in the binary log. When you use binary logging for data synchronization and a table in the source database has TTL enabled, key value conflicts might occur when data is synchronized to the secondary database.

Syntax

You can use CREATE TABLE or ALTER TABLE statements to configure TTL for a table.

Create a table with TTL properties

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

A table named t1 is created with created_at specified as its TTL column, which represents the data creation time. Set the TTL of created_at to determine whether data is deleted.

  • Set the maximum TTL of 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 sets the maximum TTL of rows in the table to 100 seconds. Expired data that exceeds this duration is deleted later.

  • Set the maximum TTL of 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
    • By setting TTL ='created_at'+ INTERVAL 3 HOUR, you can set the maximum TTL of data in the table to 3 hours. Expired data that exceeds this duration is automatically deleted.

    • This TTL setting supports multiple time units, including YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, and SECOND. This flexibility lets you select an appropriate time unit for efficient data management and maintenance based on your requirements.

Modify the TTL properties of a table

You can use either of the following methods to modify the TTL properties of a table:

In the t1 table, you can modify the TTL of created_at to determine whether data is deleted.

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

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

    Setting the TTL of the created_at field to 10,000 seconds makes data valid for 10,000 seconds after its creation time. Data older than 10,000 seconds is automatically deleted.

  • Set the maximum TTL of created_at to 3 days.

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

    Use the created_at timestamp in the TTL properties and add a validity period of 3 days to set data expiration to 3 days. This makes sure that data does not exceed 3 days after creation. Data older than 3 days is automatically deleted.

Clear the TTL properties of a table

ALTER TABLE t1 TTL = '';

Query the TTL properties of a table

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

Use the preceding statement to query whether a table contains TTL information.

Parameters

The following global parameters are also added to control the cleanup of TTL-expired data:

Note

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

Parameter name

Description

loose_innodb_enable_ttl_purge

Specifies whether to enable the cleanup of TTL-expired data.

Valid values:

  • ON: Enables the cleanup of TTL-expired data.

  • OFF (default): Disables the cleanup of TTL-expired data.

Note

You must enable the cleanup of TTL-expired data in the console.

loose_innodb_ttl_min_interval

The minimum allowed duration when you set a data expiration time. The default value is 100. The default unit is second.

loose_innodb_ttl_purge_thread

The number of threads for cleaning up TTL-expired data. After modification, reset the innodb_enable_ttl_purge parameter for the change to take effect. The default value is 4.

loose_innodb_ttl_cluster_index_purge_batch_size

If the specified TTL column has no index, the primary key is scanned when TTL-expired data is scanned. The default number of primary key rows scanned at a time is 10,000.

loose_innodb_ttl_index_purge_batch_size

If the specified TTL column has an index, the index is scanned when TTL-expired data is scanned. The default number of index rows scanned at a time is 500.

loose_innodb_ttl_purge_start_hour

The start time of the TTL cleanup task. The default value is 0. The value ranges from 0 to 23 and cannot exceed innodb_ttl_purge_end_hour.

loose_innodb_ttl_purge_end_hour

The end time of the TTL cleanup task. The default value is 0. The value ranges from 0 to 23 and cannot be less than innodb_ttl_purge_start_hour.

loose_innodb_ttl_finished_job_expired_days

Controls the expiration time of data in the mysql. ttl_job_history table. The default value is 90. The value ranges from 1 to 365. The default unit is day.

Monitor TTL

The system periodically collects runtime information for TTL and provides a system table that shows the detailed execution status of TTL cleanup tasks. The following table describes the fields in the mysql.ttl_job_history table:

Column name

Description

job_id

The ID of the TTL cleanup task, which is typically a timestamp in milliseconds.

table_name

The name of the table associated with this TTL task.

state

The execution status of the TTL task: pending, executing, or completed.

start_time

The task start time.

finished_time

The task completion time.

expire_time

The expiration time of data cleaned up by this TTL task.

scan_cost

The time required to scan this batch.

purge_cost

The time required to clean up this batch.

purge_rows

The number of data rows cleaned up by this TTL task.