All Products
Search
Document Center

Tair (Redis® OSS-Compatible):Periodically delete expired keys with OOS

Last Updated:Jun 21, 2026

Tair (Redis OSS-compatible) instances gradually delete expired data, causing it to occupy memory even after expiration. You can configure a Scheduled O&M task in CloudOps Orchestration Service (OOS) to scan (SCAN) all keys during off-peak hours, immediately delete expired keys, and reclaim memory.

Background

To ensure read and write performance, Tair (Redis OSS-compatible) instances use an asynchronous deletion logic. When data expires, the instance does not immediately delete it. Instead, expired keys are removed gradually through active deletion (a background process that periodically scans and deletes a portion of expired keys) and passive deletion (a key is deleted when a client tries to access it). In extreme cases, when a large number of keys expire at once, reclaiming the memory can take a significant amount of time.

You can use the free OOS service to periodically call the FlushExpireKeys API, which scans all keys and immediately deletes any that have expired.

Important

The SCAN command scans all data and increases the CPU load of the instance. Run this task during off-peak hours.

Prerequisites

Create a RAM role for the OOS service to access the Tair (Redis OSS-compatible) service. For more information, see Set up and grant permissions to a RAM role for OOS.

  • You can grant the AliyunKvstoreFullAccess system policy to the role, which allows it to manage all Tair (Redis OSS-compatible) resources.

  • Alternatively, you can create a custom policy based on the principle of least privilege. This policy allows OOS to call only the DescribeInstances and FlushExpireKeys APIs of Tair (Redis OSS-compatible), thereby limiting the role's permissions to this task.

    Example RAM role policy:

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "kvstore:DescribeInstances",
                    "kvstore:FlushExpireKeys"
                ],
                "Resource": "*"
            }
        ]
    }

Procedure

  1. Go to the OOS console.

  2. In the left-side navigation pane, choose Automated Task > Scheduled O&M.

  3. Click Create.

  4. Configure the Scheduled O&M task.

    Note

    This topic describes only the key parameters. For details on other parameters, see CloudOps Orchestration Service.

    Schedule settings

    In this example, select Executed Periodically to run the task at 04:00 and 16:00 every day.

    For Repeat Cycle, select cron expression and configure the fields as follows: set Minute to 0, Hour to 4,16, Day-of-month to ?, Month to *, and Day-of-week to *. For Time Zone, select (GMT+08:00) Asia/Shanghai. Set Effective Time to 2025-07-23 00:00:00.

    Set End Time to 2026-01-01 00:00:00. The scheduled O&M task stops running at this time.

    Select a template

    In the Public Templates section, search for and select the ACS-Redis-FlushExpireKeys template.

    Configure template parameters

    1. Select your instance's region, and then select the target instance.

    2. For Permission Source For Execution, select the RAM role you created in the Prerequisites section.

    Execution settings (optional)

    You can keep the default settings on this tab.

  5. Click Create, and then click OK in the confirmation dialog box.

    When a task's Execution Status is Active, it will be automatically executed according to the Schedule Settings.

Next steps

In the Scheduled O&M task list, you can click a task's Execution ID to open its details page. On this page, you can view or modify the task configuration, immediately trigger an execution, and view the execution history.

After the expired data is deleted and memory is reclaimed, you will see the instance's Memory Usage metric decrease in Performance Monitoring.

Related documentation

  • You can also configure Alert and Event O&M in OOS. For example, you can call the FlushExpireKeys API to delete expired keys when the average memory usage reaches 90%. The configuration process is similar to the one for this solution, and the entry point is Automated Task > Alert and Event O&M. For more information, see Create an alert-based O&M task.

  • For more information about how Tair and Redis delete expired data, see How do Tair and Redis purge expired keys?