All Products
Search
Document Center

Object Storage Service:Configure lifecycle rules

Last Updated:Mar 20, 2026

Lifecycle rules let you automatically expire and delete objects and incomplete multipart upload parts in an OSS on CloudBox bucket based on each object's last modified time. With lifecycle rules, you can:

  • Delete objects after a set number of days or on a specific date

  • Remove incomplete multipart upload parts that accumulate over time

  • Reduce storage costs without manual intervention

Prerequisites

Before you begin, ensure that you have:

  • OSS on CloudBox available in your region — supported regions: China (Hangzhou), China (Shanghai), China (Shenzhen), China (Heyuan), China (Beijing), and China (Chengdu)

  • A cloud box purchased. See Purchase a cloud box

  • A Virtual Private Cloud (VPC) and a vSwitch created in the OSS on CloudBox. See Create a VPC and a vSwitch

  • A VPC internal network set up with a single tunnel configured for secure connection. To apply for this feature, contact technical support

How lifecycle rules work

When rules take effect

OSS loads a new lifecycle rule within 24 hours of creation. After loading, the rule runs once per day at 08:00 (UTC+8).

The interval between an object's last modified time and rule execution must exceed 24 hours. For example, with a rule set to delete objects 1 day after upload, objects uploaded on July 20, 2020 are deleted as follows:

  • Uploaded before 08:00 (UTC+8) on July 20: deleted between 08:00 July 21 and 08:00 July 22

  • Uploaded after 08:00 (UTC+8) on July 20: deleted between 08:00 July 22 and 08:00 July 23

Important

When you update a lifecycle rule, tasks already scheduled for the current day are suspended. Avoid updating lifecycle rules frequently.

Completion time

The number of lifecycle actions OSS can complete within 24 hours depends on the rule scope and region.

Rules without tags

RegionCapacity per 24 hours
China (Hangzhou), China (Shanghai), China (Beijing), China (Zhangjiakou), China (Ulanqab), China (Shenzhen), SingaporeUp to 1 billion actions
All other regionsUp to 100 million actions

Rules with tags

RegionCapacity per 24 hours
China (Hangzhou), China (Shanghai), China (Beijing), China (Zhangjiakou), China (Ulanqab), China (Shenzhen), SingaporeUp to 500 million actions
All other regionsUp to 50 million actions

If the number of actions exceeds the capacity, completion may take more than 24 hours.

Note

When versioning is enabled, each action on an individual object version counts toward these limits.

Overwrite semantics

PutBucketLifecycle overwrites all existing lifecycle rule configurations for a bucket. To add a new rule without losing existing ones:

  1. Call GetBucketLifecycle to retrieve existing rules.

  2. Add the new rule to the retrieved list.

  3. Call PutBucketLifecycle with the complete list.

Create a lifecycle rule

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. Click the OSS on CloudBox bucket for which you want to configure lifecycle rules.

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

  5. On the Lifecycle page, click Create Rule and configure the parameters. For unversioned buckets, configure the following parameters: For versioning-enabled buckets, configure the Basic Settings and Delete Parts sections the same way as for unversioned buckets. The following parameters differ:

    Important

    Each lifecycle rule must include at least one of: an object expiration policy or a part expiration policy.

    Note

    The previous version expiration period starts from the last modified time of the next version—not from when the object was originally created.

    SectionParameterDescription
    Basic SettingsStatusEnable or disable the rule. Valid values: Enabled, Disabled
    Basic SettingsApplied ToScope of the rule. Select Files with Specified Prefix to apply the rule to objects matching a prefix, or Whole Bucket to apply it to all objects
    Basic SettingsPrefixThe prefix to match. For example, enter img to apply the rule to all objects whose names start with img. Required when Applied To is set to Files with Specified Prefix
    Clear PolicyObject LifecycleWhen objects expire. Valid values: Validity Period (Days), Expiration Date, Disabled. Select Disabled to skip object expiration
    Clear PolicyDeleteWhen enabled, expired objects are automatically deleted and cannot be recovered
    Delete PartsPart LifecycleWhen parts expire. Valid values: Validity Period (Days), Expiration Date, Disabled. Select Disabled to skip part expiration
    Delete PartsDeleteWhen enabled, expired parts are automatically deleted and cannot be recovered
    SectionParameterDescription
    Current VersionRemoval of Delete MarkerWhen enabled (Clean Up Delete Marker), OSS removes a delete marker only if it is the sole remaining version of an object. If the object has multiple versions and the current version is a delete marker, OSS retains it. See Delete marker
    Previous VersionsObject LifecycleWhen previous versions expire. Valid values: Validity Period (Days), Disabled
    Previous VersionsDeleteThe number of days a previous version is retained before deletion. For example, with a validity period of 30 days, an object that became a previous version on September 1, 2021 is deleted on October 1, 2021

Use OSS SDK for Java

OSS SDK for Java version 3.15.0 or later is required to configure lifecycle rules for an OSS on CloudBox bucket.

The following example creates a lifecycle rule (rule0) that expires all objects in the bucket 3 days after they are last modified, then queries the configured rules.

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 {
        // Endpoint of the OSS on CloudBox bucket
        String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
        // Load credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET
        EnvironmentVariableCredentialsProvider credentialsProvider =
            CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Name of the OSS on CloudBox bucket, e.g., examplebucket
        String bucketName = "examplebucket";
        // Region where the OSS on CloudBox bucket is located
        String region = "cn-hangzhou";
        // ID of the cloud box
        String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";

        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 {
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);

            String ruleId0 = "rule0";
            // Null prefix: the rule applies to all objects in the bucket
            String matchPrefix0 = null;

            // Expire objects 3 days after last modification
            LifecycleRule rule = new LifecycleRule(ruleId0, matchPrefix0, LifecycleRule.RuleStatus.Enabled, 3);
            request.AddLifecycleRule(rule);

            ossClient.setBucketLifecycle(request);

            // Query the configured lifecycle rules
            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("OSS error: " + oe.getErrorMessage()
                + " (Code: " + oe.getErrorCode()
                + ", RequestId: " + oe.getRequestId() + ")");
        } catch (ClientException ce) {
            System.out.println("Client error: " + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

Before running the code, set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables to your Alibaba Cloud credentials, and replace the following placeholders:

PlaceholderDescriptionExample
cb-f8z7yvzgwfkl9q0h****Cloud box IDcb-f8z7yvzgwfkl9q0h1234
examplebucketOSS on CloudBox bucket namemy-cloudbox-bucket
cn-hangzhouRegion IDcn-shanghai

Use ossutil

See put-bucket-lifecycle for the ossutil command reference.

Use the RESTful API

For direct API calls, see PutBucketLifecycle. Direct API calls require you to handle signature calculation in your code.

Quotas and limits

Number of lifecycle rules

The OSS console supports up to 100 lifecycle rules per OSS on CloudBox bucket. To configure more than 100 rules, use OSS SDKs or ossutil.

Overlapping prefixes for part policies

Two or more lifecycle rules cannot both contain a part policy when their target prefixes overlap:

  • A bucket-level part policy (no prefix) blocks any other part policy for that bucket

  • A part policy for prefix dir1 blocks any other part policy targeting dir1/dir2 or any other prefix that overlaps with dir1

Billing

Using lifecycle rules to convert the storage class of objects or delete objects may incur request fees and storage fees. See Fees related to lifecycle rules.

What's next