Updated on 2025-06-30 GMT+08:00

Adding an Image Retention Policy

Scenarios

You can add a retention policy to an image in SWR to automatically delete any unused image tags. The policy takes effect immediately after you set it. There are two types of policies:

  • Number of days: keeping only image tags that have been pushed to SWR within a certain number of days.
  • Number of tags: keeping only a certain number of the most recent image tags.

You can configure filters for your retention policy to prevent certain image tags from being affected by the retention policy.

Constraints

Only one retention rule can be added to an image. If you want to add a new retention policy, you must delete the existing policy.

Procedure

  1. Log in to the SWR console.
  2. In the navigation pane, choose My Images. Then click the name of the target image to go to its details page.
  3. On the Retention tab page, click Add Retention Policy. Configure the policy based on Table 1 and click OK.

    Figure 1 Adding a retention policy
    Table 1 Parameters for adding an image retention policy

    Parameter

    Description

    Type

    There are two types of retention policies:

    • Number of days: keeping only image tags that have been pushed to SWR within a certain number of days.
    • Number of tags: keeping only a certain number of the most recent image tags.

    Retention Days

    This parameter is available only when Type is set to Number of days. It indicates the number of the most recent days for which the images will be retained. The value must be an integer ranging from 1 to 365.

    Count Limit

    This parameter is available only when Type is set to Number of tags. It indicates the number of the most recent image tags to be retained. The value must be an integer ranging from 1 to 1,000.

    Filter with Tags

    Enter the image tags that need to be excluded from the retention policy.

    Filter with Regular Expression

    Enter a regular expression. Image tags matching this regular expression will be excluded from the retention policy.

    After the retention policy is added, SWR immediately applies the policy and displays removed image tags (if any) in the Retention Logs area.

    Figure 2 Checking the retention policy and logs

Example 1: Set Policy Type to Number of Days

The image nginx has two tags: v1 and v2. The following figure shows when they were updated.

Figure 3 Image tags
  1. Add a retention policy.

    Set Type to Number of days, and Retention Days to 3.

    Figure 4 Adding a retention policy
  2. Check whether the retention policy has taken effect.

    Check Retention Logs. Nginx image v1 has been stored for more than three days (the current time is 2021/09/01 16:00:00). So, it is automatically removed.

    Check Image Tag. Only Nginx image v2 is left.

    Figure 5 Image v2

    The retention policy has taken effect.

Example 2: Set Policy Type to Number of Tags and Use a Regular Expression Filter

Click the Image Tags tab. The image nginx has four tags: v1, v2, v1.0.0, and v2.0.0 as shown in the following figure.

Figure 6 Nginx image tags
  1. Add a retention policy.

    Set Type to Number of tags, Count Limit to 1, and Filter with Regular Expression to ^v2.*.

    Figure 7 Adding a retention policy
  2. Check whether the retention policy has taken effect.

    Based on the regular expression, Nginx image v2 and v2.0.0 will be excluded from the policy. In the policy, the count is 1, so only one of Nginx image v1 and v1.0.0 can be retained. Nginx image v1 will be removed because it is older.

    Check Image Tag in Retention Logs. If Nginx image v1 is removed, the retention policy has taken effect.

    Figure 8 Image tags

    The following regular expressions are for your reference:

    • ^[0-9]*$: filters out tags consisting of numbers.
    • ^.{2,5}$: filters out tags with a length ranging from 2 to 5 characters.
    • ^[a-z]+$: filters out tags consisting of lowercase letters.
    • ^[A-Za-z0-9]+$: filters out tags consisting of letters and numbers.

    If there is an OR (|) operator in a regular expression, enclose the OR part in parentheses, or the regular expression will be parsed incorrectly and all tags of the image will be removed.

    For example, if you only want to retain tags containing a or s, the regular should be (.*a.*|.*s.*).