All Products
Search
Document Center

Retention policies

Last Updated: Jun 19, 2022

Object Storage Service (OSS) supports the Write Once Read Many (WORM) feature. The feature helps prevent objects from being deleted or overwritten within a specified period of time. Enterprises use this feature to comply with the regulations of the U.S. Securities and Exchange Commission (SEC) and Financial Industry Regulatory Authority. (FINRA).

OSS provides strong compliance policies. You can configure time-based retention policies for OSS buckets. After you configure and lock a retention policy for a bucket, you can upload objects to or read objects from the bucket. However, you cannot delete objects in the bucket or the retention policy within the retention period that is specified in the retention policy. You can delete the objects only after the retention period expires. The WORM feature is suitable for industries such as finance, insurance, health care, and securities. OSS provides the WORM feature to allow you to build a compliant bucket in the cloud.

Note

OSS is accredited and audited by Cohasset Associates and meets the requirements for electronic data storage. OSS buckets for which retention policies are configured can be used for business that is subject to regulations such as SEC Rule 17a-4(f), CFTC Rule 1.31(c)-(d), and FINRA Rule 4511(c). For more information, see OSS Cohasset Assessment Report.

Limits

  • You cannot configure retention policies in the China (Guangzhou), China (Nanjing - Local Region), and US (Virginia) regions.

  • You can configure retention policies only for buckets in OSS.

  • A bucket cannot have versioning and retention policies at the same time. If versioning is enabled for a bucket, you cannot configure retention policies for the bucket. For more information about versioning, see Overview.

  • We recommend that you do not enable the OSS-HDFS service and configure retention policies for a bucket at the same time.

    If you enable the OSS-HDFS service and configure a retention policy for a bucket, and then delete data from the .dlsdata/ directory by using the methods that are supported by the OSS-HDFS service, a message that indicates that the data is successfully deleted is displayed. However, OSS still retains the deleted data in the retention period that is specified for the retention policy and OSS cannot recognize and delete the data after the retention period expires.

  • During the retention period, you can configure lifecycle rules to convert the storage classes of the objects in the bucket. This way, you can reduce costs and ensure compliance. For more information, see Lifecycle rules based on the last modified time.

Rules

You can configure only one time-based retention policy for a bucket. The policy specifies a retention period that ranges from one day to 70 years.

For example, you created a bucket named examplebucket on June 1, 2013, and uploaded the file1.txt, file2.txt, and file3.txt objects to the bucket at different points in time. On July 1, 2014, you created a retention policy in which a five-year retention period is specified. The following table describes the upload and expiration dates of the objects.

Object

Upload date

Expiration date

file1.txt

June 1, 2013

May 31, 2018

file2.txt

July 1, 2014

June 30, 2019

file3.txt

September 30, 2018

September 29, 2023

  • Implementation rules

    By default, a time-based retention policy is in the InProgress state after the policy is created for a bucket. The InProgress state remains valid for 24 hours. The retention policy protects the resources in the bucket within 24 hours after the policy is created.

    • Within 24 hours after the retention policy is created: If the retention policy is not locked, the bucket owner and authorized users can delete the policy. If the retention policy is locked, the retention period of the policy cannot be shortened and the policy cannot be deleted. The retention period can only be extended.

    • 24 hours after the retention policy is created: If the retention policy is not locked, the policy becomes invalid.

    • If you attempt to delete or modify data in the protected bucket, the OSS API operation returns the 409 FileImmutable error.

  • Deletion rules

    • A time-based retention policy is a metadata attribute of a bucket. If a bucket is deleted, the retention policy and the access control list (ACL) of the bucket are also deleted. You can delete a bucket only when the bucket is empty.

    • If the retention policy is not locked within 24 hours after the policy is created, the bucket owner and authorized users can delete the policy.

    • If a bucket contains objects that are protected within the retention period, you cannot delete the bucket or the retention policy.

Use the OSS console

    Use OSS SDKs

    The following sample code provides an example on how to configure retention policies by using OSS SDKs for common programming languages. For more information about the sample code that is used to configure retention policies by using OSS SDKs for other programming languages, see Overview.

    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.InitiateBucketWormRequest;
    import com.aliyun.oss.model.InitiateBucketWormResult;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
            String accessKeyId = "yourAccessKeyId";
            String accessKeySecret = "yourAccessKeySecret";
            // Specify the name of the bucket. Example: examplebucket. 
            String bucketName = "examplebucket";
    
            // Create an OSSClient instance. 
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
            try {
                // Create an InitiateBucketWormRequest object. 
                InitiateBucketWormRequest initiateBucketWormRequest = new InitiateBucketWormRequest(bucketName);
                // Set the retention period to one day. 
                initiateBucketWormRequest.setRetentionPeriodInDays(1);
    
                // Create a retention policy. 
                InitiateBucketWormResult initiateBucketWormResult = ossClient.initiateBucketWorm(initiateBucketWormRequest);
    
                // Query the ID of the retention policy. 
                String wormId = initiateBucketWormResult.getWormId();
                System.out.println(wormId);
            } 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();
                }
            }
        }
    }
    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    
    try {
        // Create a retention policy and set the retention period to 30 days. 
        $wormId = $ossClient->initiateBucketWorm($bucket, 30);
    
        // Query the ID of the retention policy. 
        print($wormId);
    } catch (OssException $e) {
        printf(__FUNCTION__ . ": FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
    
    print(__FUNCTION__ . ": OK" . "\n");
    const OSS = require('ali-oss');
    
    const client = new OSS({
      // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
      region: 'yourregion',
      // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret'
    });
    // Create a retention policy. 
    async function initiateBucketWorm() {
     // Specify the name of the bucket. Example: examplebucket. 
      const bucket = 'examplebucket'
      // Specify the retention period of objects. 
      const days = '30'
            const res = await client.initiateBucketWorm(bucket, days)
      console.log(res.wormId)
    }
    
    initiateBucketWorm()
    # -*- coding: utf-8 -*-
    import oss2
    
    # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    # Specify the name of the bucket. Example: examplebucket. 
    bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
    
    # Create the retention policy and set the retention period to one day. 
    result = bucket.init_bucket_worm(1)
    # Query the ID of the retention policy. 
    print(result.worm_id)
    package main
    
    import (
        "fmt"
        "github.com/aliyun/aliyun-oss-go-sdk/oss"
        "os"
    )
    
    func HandleError(err error) {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    
    func main() {
        // Create an OSSClient instance. 
        // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
        client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
        if err != nil {
            HandleError(err)
        }
    
        // Specify the name of the bucket. Example: examplebucket. 
        bucketname := "examplebucket"
        // Set the retention period to 60 days. 
        result,err := client.InitiateBucketWorm(bucketname,60)
        if err != nil {
            HandleError(err)
        }
    
        fmt.Println(result)
    }
    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
          /* Initialize information about the account that is used to access OSS. */
          /* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
          std::string AccessKeyId = "yourAccessKeyId";
          std::string AccessKeySecret = "yourAccessKeySecret";
          /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
          std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
          /* Specify the name of the bucket. Example: examplebucket. */
          std::string BucketName = "examplebucket";
    
          /* Initialize resources such as networks. */
          InitializeSdk();
    
          ClientConfiguration conf;
          OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
      
            /* Create a retention policy and set the retention period to 10 days. */
          auto outcome = client.InitiateBucketWorm(InitiateBucketWormRequest(BucketName, 10));
    
          if (outcome.isSuccess()) {      
                std::cout << " InitiateBucketWorm success " << std::endl;
          }
          else {
            /* Handle exceptions. */
            std::cout << "InitiateBucketWorm fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
          }
    
          /* Release resources such as network resources. */
          ShutdownSdk();
          return 0;
    }

    Use ossutil

    For more information about how to configure retention policies by using ossutil, see worm (manage retention policies).

    Use the RESTful API

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

    FAQ

    • What are the benefits of a retention policy?

      A retention policy can be used to meet data security standards. Within the retention period of a retention policy, data cannot be modified or deleted. The data that is protected by using RAM policies and bucket policies can be modified and deleted.

    • What are the scenarios in which a retention policy can be used?

      You can use a retention policy if you want to store important data that is infrequently accessed and not allowed to be modified or deleted. This type of data includes medical records, technical documents, and contracts. You can store these archived objects in a specific bucket and configure a retention policy for the bucket.

    • Does a retention policy take effect on individual objects?

      Retention policies can be configured only at the bucket level. OSS does not support retention policies at the directory or object level.

    • How do I calculate the expiration time of an object?

      To calculate the time when an object expires, add the retention period and the time when the object was last updated. For example, the retention policy for Bucket A specifies the retention period as 10 days. An object in the bucket was last updated at 12:00 on February 15, 2022. The object expired at 12:01 on February 25, 2022.

    • How do I delete a bucket that is protected by a retention policy?

      • If the bucket contains no objects, the bucket can be deleted.

      • If the bucket contains objects, the bucket cannot be deleted even if the retention period elapses. Before you can delete the bucket, you must delete all objects in the bucket.

      • If the bucket contains objects that are protected within the retention period, the bucket cannot be deleted.

    • Are objects that are protected within the retention period of the retention policy retained if my account has overdue OSS payments?

      If your account has overdue payments, Alibaba Cloud retains data based on the terms and conditions of your contract.

    • Can an authorized RAM user configure a retention policy?

      All API operations related to the retention policy are available. These API operations support RAM policies. RAM users that are granted permissions by using RAM policies can create or delete a retention policy by using the OSS console, API operations, or SDKs.