All Products
Search
Document Center

Object Storage Service:Configure lifecycle rules

Last Updated:Nov 23, 2025

You can configure lifecycle rules based on the last modified time of objects to regularly delete objects and parts from an Object Storage Service (OSS) on CloudBox bucket to reduce storage costs.

Prerequisites

  • OSS on CloudBox is supported only in the China (Hangzhou), China (Shanghai), China (Shenzhen), China (Heyuan), China (Beijing), and China (Chengdu) regions.

  • A cloud box is purchased. For more information, see Purchase a cloud box.

  • A Virtual Private Cloud (VPC) and a vSwitch are created in the OSS on CloudBox. For more information, see Create a VPC and a vSwitch.

  • A VPC internal network is set up, and a single tunnel is configured to provide secure connection. To apply for this feature, please contact technical support.

Limits

You cannot configure two or more lifecycle rules that contain a part policy for objects whose names contain overlapping prefixes. Examples:

  • Example 1

    If you configure a lifecycle rule that contains a part policy for an OSS on CloudBox bucket, you cannot configure another lifecycle rule that contains a part policy for any objects in the OSS on CloudBox bucket.

  • Example 2

    If you configure a lifecycle rule that contains a part policy for objects whose names contain the dir1 prefix in an OSS on CloudBox bucket, you cannot configure another lifecycle rule that contains a part policy for objects whose names contain overlapping prefixes, such as dir1/dir2.

Usage notes

Number of lifecycle rules

You can configure up to 100 lifecycle rules for an OSS on CloudBox bucket in the OSS console. To configure more than 100 lifecycle rules for an OSS on CloudBox bucket, use OSS SDKs or ossutil.

Overwrite semantics

The PutBucketLifecycle operation overwrites the existing configurations of a lifecycle rule of a bucket. For example, if a lifecycle rule named Rule1 is configured for a bucket and you want to configure another lifecycle rule named Rule2 for the bucket, perform the following operations:

  • Call the GetBucketLifecycle operation to query Rule1.

  • Add both Rule1 and Rule2 to the lifecycle rule configuration.

  • Call the PutBucketLifecycle operation to create Rule1 and Rule2 for the bucket.

Effective time

OSS loads a lifecycle rule within 24 hours after the rule is created. After the lifecycle rule is loaded, OSS runs the rule every day at 08:00:00 (UTC+8).

The interval between the last modified time of an object and the time when the lifecycle rule is run must be longer than 24 hours. For example, if you configure a lifecycle rule for a bucket to delete objects one day after they are uploaded, objects that are uploaded on July 20, 2020 are deleted on a different date based on the specific time when the objects are uploaded.

  • Objects uploaded before 08:00:00 (UTC+8) on July 20, 2020 are deleted from 08:00:00 (UTC+8) on July 21, 2020 to 08:00:00 (UTC+8) on July 22, 2020.

  • Objects uploaded after 08:00:00 (UTC+8) on July 20, 2020 are deleted from 08:00:00 (UTC+8) on July 22, 2020 to 08:00:00 (UTC+8) on July 23, 2020.

Important

When you update a lifecycle rule, tasks that are scheduled to be performed based on the rule on the current day are suspended. We recommend that you do not frequently update lifecycle rules.

Completion time

Completion time

Lifecycle rule that does not contain tags

  • For a lifecycle rule that is not based on tags, up to 1 billion lifecycle management actions, including object deletion, storage class conversion, and part expiration, can be completed within 24 hours in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), China (Zhangjiakou), China (Ulanqab), China (Shenzhen), and Singapore. If the number of lifecycle management actions based on the lifecycle rule exceeds 1 billion, the time required to complete the actions may exceed 24 hours.

  • For a lifecycle rule that is not based on tags, up to 100 million lifecycle management actions can be completed within 24 hours in other regions. If the number of lifecycle management actions based on the lifecycle rule exceeds 100 million, the time required to complete the actions may exceed 24 hours.

Lifecycle rule that contains tags

  • For a lifecycle rule that is based on tags, up to 500 million lifecycle management actions, including object deletion, storage class conversion, and part expiration, can be completed within 24 hours in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), China (Zhangjiakou), China (Ulanqab), China (Shenzhen), and Singapore. If the number of lifecycle management actions based on the lifecycle rule exceeds 500 million, the time required to complete the actions may exceed 24 hours.

  • For a lifecycle rule that is based on tags, up to 50 million lifecycle management actions can be completed within 24 hours in other regions. If the number of lifecycle management actions based on the lifecycle rule exceeds 50 million, the time required to complete the actions may exceed 24 hours.

Note

If versioning is enabled for a bucket, a lifecycle management action on each object version in the bucket is counted towards the applicable limit.

Billing rules

If you use lifecycle rules to convert the storage class of objects or delete objects, you may be charged request fees and storage fees. For more information, see Fees related to lifecycle rules.

Procedure

Use the OSS console

  1. Log on to the OSS console.

  2. In the left-side navigation pane, choose Data Service > OSS on CloudBox Buckets.

  3. On the OSS on CloudBox Buckets page, click the OSS on CloudBox bucket for which you want to enable configure lifecycle rules.

  4. In the left-side navigation tree, choose Data Management > Lifecycle.

  5. On the Lifecycle page, click Create Rule. In the Create Rule panel, configure the parameters. The following table describes the parameters.

    • Unversioned OSS on CloudBox bucket

      Section

      Parameter

      Description

      Basic Settings

      Status

      Specify the status of the lifecycle rule. Valid values: Enabled and Disabled.

      Applied To

      Specify the objects to which the lifecycle rule applies. Valid values: Files with Specified Prefix and Whole Bucket.

      Prefix

      Specify the prefix in the names of the objects for which you want the lifecycle rule to take effect. For example, if you want the rule to take effect for objects whose names start with img, enter img.

      Clear Policy

      Object Lifecycle

      Configure rules for objects to specify when the objects expire. Valid values: Validity Period (Days), Expiration Date, and Disabled. If you select Disabled, the configurations of File Lifecycle do not take effect.

      Delete

      Delete expired objects.

      For example, if you set the Expiration Date parameter to Sept 24, 2022 and specify this parameter, objects that were last modified before September 24, 2022 are automatically deleted. Deleted objects cannot be recovered.

      Delete Parts

      Part Lifecycle

      Specify the operations that you want to perform on expired parts. You can select Validity Period (Days), Expiration Date, or Disabled. If you select Disabled, the part lifecycle configuration is not enabled.

      Important Each lifecycle rule must contain at least one of object expiration policies and part expiration policies.

      Delete

      Specify when parts expire based on the value of Part Lifecycle. Expired parts are automatically deleted and cannot be recovered.

    • Versioning-enabled OSS on CloudBox bucket

      Configure the parameters in the Basic Settings and Delete Parts sections in the same manner as you configure the parameters for an unversioned OSS on CloudBox bucket. The following table describes only the parameters that are different from the parameters that you configure for an unversioned OSS on CloudBox bucket.

      Section

      Parameter

      Description

      Current Version

      Removal of Delete Marker

      If you enable versioning for the bucket, you can configure the Clean Up Delete Marker parameter. Other parameters are the same as those you can configure for unversioned buckets.

      If you select Clean Up Delete Marker, and an object has only one version which is a delete marker, OSS considers the delete marker expired and removes the delete marker. If an object has multiple versions and the current version of the object is a delete marker, OSS retains the delete marker. For more information about delete makers, see Delete marker.

      Previous Versions

      Object Lifecycle

      Specify when previous versions expire. Valid values: Validity Period (Days) and Disabled. If you select Disabled, File Lifecycle does not take effect.

      Delete

      Specify the number of days for which objects can be retained after they become previous versions. After the previous versions expire, the previous versions are deleted the next day. For example, if you set Validity Period (Days) to 30, objects that became previous versions on September 1, 2021 are deleted on October 1, 2021.

      Important You can determine when an object becomes a previous version based on the time when the next version of the object is last modified.

Use OSS SDK for Java

You can configure lifecycle rules for an OSS on CloudBox bucket only by using OSS SDK for Java. The version of OSS SDK for Java must be 3.15.0 or later.

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.LifecycleRule;
import com.aliyun.oss.model.SetBucketLifecycleRequest;
import java.util.List;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Specify the data endpoint of the OSS on CloudBox bucket. 
        String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the name of the OSS on CloudBox bucket. Example: examplebucket. 
        String bucketName = "examplebucket";
    // Specify the region in which the OSS on CloudBox bucket is located. 
    String region = "cn-hangzhou";
    // Specify the ID of the cloud box. 
        String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";

        // Create an OSSClient instance.
        // Call the shutdown method to release resources when the OSSClient is no longer in use. 
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
                .clientConfiguration(conf)
                .region(region)
                .build();

        try {
            // Create a request by using SetBucketLifecycleRequest. 
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);

            // Specify the ID of the lifecycle rule. 
            String ruleId0 = "rule0";
            // If the prefix is left empty, the lifecycle rule take effect for all objects in the OSS on CloudBox bucket. 
            String matchPrefix0 = null;

            // Specify that objects expire three days after they are last modified. 
            LifecycleRule rule = new LifecycleRule(ruleId0, matchPrefix0, LifecycleRule.RuleStatus.Enabled, 3);
            request.AddLifecycleRule(rule);

            // Initiate a request to configure the lifecycle rule. 
            ossClient.setBucketLifecycle(request);

            // Query the lifecycle rule. 
            List<LifecycleRule> listRules = ossClient.getBucketLifecycle(bucketName);
            for(LifecycleRule rules : listRules){
                System.out.println("ruleId="+rules.getId()+", matchPrefix="+rules.getPrefix()+", expirationDays="+rules.getExpirationDays());
            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

Use ossutil

You can configure lifecycle rules for an OSS on CloudBox bucket by using ossutil. For more information, see put-bucket-lifecycle.

Use the RESTful API

If your business requires a high level of customization, you can directly call the RESTful APIs. To directly call an API, you must include the signature calculation in your code. For more information, see PutBucketLifecycle.