All Products
Document Center

ApsaraDB for Redis:Use OOS to periodically delete expired keys of Tair or Redis

Last Updated:Mar 12, 2024

When Redis data expires, Redis gradually deletes the expired data based on the default policy. As a result, the expired data still occupies memory space. You can configure a scheduled O&M task in the CloudOps Orchestration Service (OOS) console to scan all keys during off-peak hours by running the SCAN command and immediately delete expired keys to free up memory.

Background information

To ensure read and write performance, Redis employs the asynchronous deletion logic. When data expires, Redis does not immediately delete the expired data. Instead, it gradually removes expired keys through a combination of active deletion (regular background scanning and deletion of a portion of expired keys) and passive deletion (deletion upon user access to an expired key). Expired data still occupies memory space. In extreme cases, when a large volume of data expires simultaneously, the data becomes expired immediately, but deleting the data and reclaiming memory space occupied by the data may take a significant amount of time.

You can refer to the procedure in this topic to use the OOS service for free to periodically call the FlushExpireKeys operation. Redis runs the SCAN command to scan all keys and immediately deletes expired keys.


Running the SCAN command to scan all data can cause an increase in CPU load for the instance. We recommend that you run this command during off-peak hours.


A Resource Access Management (RAM) role is created for OOS to access Redis. For more information, see Grant RAM permissions to OOS.

  • You can attach system policy AliyunKvstoreFullAccess to the role. After permissions defined in the policy are granted to the role, the role can manage all resources of Redis.

  • You can also create a custom policy that includes the minimum permissions required to allow OOS to call the DescribeInstances and FlushExpireKeys operations of Redis. This policy restricts the role to only the current task.

    Example of a policy attached to a RAM role:

        "Version": "1",
        "Statement": [
                "Effect": "Allow",
                "Action": [
                "Resource": "*"


  1. Log on to the OOS console.

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

  3. Click Create.

  4. Configure a scheduled O&M task.


    This topic introduces only the key steps. For information about other parameters, see What is OOS?

    Set Scheduled Task

    In this example, Executed Periodically is selected and the task is set to execute at 04:00 and 16:00 every day.


    End Time for Period Execution is set to 2024-03-31 00:00:00, which indicates that the scheduled O&M task ends at this point in time.

    Select Template

    Search for ACS-Redis-FlushExpireKeys in the Public Template search box and select the template.

    Configure Template Parameters

    1. Select the region where the desired Redis instance resides and select the instance.

    2. From the Permissions drop-down list, select the RAM role that is prepared in the Prerequisites section.

    Execution Settings(Optional)

    You can leave the configuration items on this tab unchanged.

  5. Click Create. In the dialog box that appears, click OK.

    If the value of the Execution Status field is Valid, the task is automatically executed based on the settings specified on the Set Scheduled Task tab.

What to do next

In the Scheduled O&M task list, click the execution ID of the created task to go to the task details page. On this page, you can view or modify task configuration information, immediately trigger execution, and view execution history.

Additionally, because expired data has been deleted and memory has been reclaimed, you can also observe a reduction in memory usage on the instance in the performance monitoring module.


  • You can also configure Alert and Event O&M tasks in OOS. For example, you can configure an alert O&M task to call the FlushExpireKeys operation to delete expired keys when the average memory usage reaches 90%. The specific procedure is similar to the procedure in this topic. You can choose Automated Task > Alert and Event O&M to configure an alert O&M task. For more information, see Create an alert O&M task.

  • For more information about how to delete expired data in Tair and Redis, see How do I delete expired keys in ApsaraDB for Redis?