You can call this operation to configure lifecycle rules for a bucket. After you configure lifecycle rules for a bucket, OSS automatically deletes the objects that match the lifecycle rules. Only the owner of a bucket can initiate a PutBucketLifecycle request for the bucket.

Note
  • If no lifecycle rules are configured for a bucket, the PutBucketLifecycle operation creates a lifecycle rule for the bucket. If a lifecycle rule has been configured for the bucket, this operation creates a lifecycle rule that overwrites the existing one.
  • PutBucketLifecycle uses the overwriting semantics. A new lifecycle rule overwrites the existing rule. To configure a new lifecycle rule for a bucket with a lifecycle rule configured, you must call the GetBucketLifecycle operation to obtain the current lifecycle configurations of the bucket, add a new lifecycle configuration, and call the PutBucketLifecycle operation to update the lifecycle configurations for the bucket.
  • You can call the PutBucketLifecycle operation to set a validity period for objects or parts that are generated in incomplete multipart upload tasks.

Request syntax

PUT /? lifecycle HTTP/1.1
Date: GMT Date
Content-Length:ContentLength
Content-Type: application/xml
Authorization: SignatureValue 
Host: BucketName.oss.aliyuncs.com
<? xml version="1.0" encoding="UTF-8"? >
<LifecycleConfiguration>
  <Rule>
    <ID>RuleID</ID>
    <Prefix>Prefix</Prefix>
    <Status>Status</Status>
    <Expiration>
      <Days>Days</Days>
    </Expiration>
    <Transition>
      <Days>Days</Days>
      <StorageClass>StorageClass</StorageClass>
    </Transition>
    <AbortMultipartUpload>
      <Days>Days</Days>
    </AbortMultipartUpload>
  </Rule>
</LifecycleConfiguration>

Request elements

Element Type Required Description
CreatedBeforeDate String Either Days or CreatedBeforeDate

Specifies a date. OSS runs lifecycle rules for objects that are last modified before this date. The date must conform to the ISO 8601 standard. The time must be in UTC. For example, 2002-10-11T00:00:00.000Z indicates that objects last updated before 2002-10-11T00: 00: 00.000Z are deleted or the storage classes of these objects are converted to another storage class. Objects last updated at or after this time are not deleted or their storage classes are not converted to another storage class.

Parent node: Expiration or AbortMultipartUpload

Days Positive integer Either Days or CreatedBeforeDate

Specifies the number of days within which objects can be retained after they are last modified.

Parent element: Expiration.

Expiration Container No
Specifies the operation on the objects for the lifecycle rule.
Note For an object in a versioning-enabled bucket, this element only specifies the validity period of the current version of the object.

Child node: Days or CreatedBeforeDate, or ExpiredObjectDeleteMarker

Parent node: Rule

AbortMultipartUpload Container No

Specifies the operation on the multipart upload tasks that are not complete.

Child node: Days or CreatedBeforeDate

Parent node: Rule

ID String No

Specifies the unique ID of a lifecycle rule. An ID is composed of up to 255 bytes. If the value of this parameter is null or not specified, OSS automatically generates a unique ID for the lifecycle rule.

Child node: none

Parent node: Rule

LifecycleConfiguration Container Yes

Specifies the container that stores lifecycle configurations. The container can contain up to 1,000 rules.

Chile node: Rule

Parent node: none

Prefix String Yes

Specifies the prefix for a rule. The rule applies only to objects whose names contain the specified prefix. The prefixes specified by different rules cannot overlap.

Child node: none

Parent node: Rule

Rule Container Yes

Specifies a rule.

Note
  • You cannot create a rule to convert the storage class of objects in an Archive bucket.
  • The validity period specified for Expiration must be longer than that specified for Transition. Likewise, the expiration time specified for Expiration must be later than that specified for Transition.

Child node: ID, Prefix, Status, and Expiration

Parent node: LifecycleConfiguration

Status String Yes

The value of Enabled indicates that OSS runs the rule. The value of Disabled indicates that OSS ignores the rule.

Parent node: Rule

Valid values: Enabled and Disabled

StorageClass String Yes if Transition or NoncurrentVersionTransition is configured

Specifies the current storage class of the object.

Note You can convert the storage class of an object in an IA bucket to Archive or ColdArchive, but not Standard.

Valid values: IA, Archive and ColdArchive

Parent node: Transition

Transition Container No

Specifies the validity period within which objects can be retained and the conversion operation on the objects. After the validity period expires, the storage class of the objects can be converted to IA, Archive or ColdArchive.

Note The storage class of standard objects can be converted to IA or Archive. Note that the validity period for conversion to Archive must be longer than that for conversion to IA. For example, if the validity period is set to 30 for objects whose storage classes are converted to IA after the validity period expires, the validity period must be set to a value greater than 30 for objects whose storage classes are converted to Archive or ColdArchive.

Parent node: Rule

Child node: Days, CreatedBeforeDate, and Storageclass
Note Either Days or CreatedBeforeDate
Tag Container No Specifies the object tag for a rule. You can set multiple tags.

Parent node: Rule

Child node: Key and Value

Key String Yes if Tag is configured Specifies the tag key.

Parent node: Tag

Value String Yes if Tag is configured Specifies the tag value.

Parent node: Tag

NoncurrentDays String Yes if NoncurrentVersionTransition or NoncurrentVersionExpiration is configured Specifies the number of days within which the previous versions can be retained. When the current versions expire, they roll back to the previous versions.

Parent node: NoncurrentVersionTransition and NoncurrentVersionExpiration

NoncurrentVersionTransition Container No Specifies the validity period within which previous versions can be retained and the conversion operation on the previous versions. After the validity period expires, the storage class of the previous versions can be converted to IA or Archive.
Note The storage class of standard objects can be converted to IA or Archive. Note that the validity period for conversion to Archive must be longer than that for conversion to IA. For example, if the validity period is set to 30 for previous versions whose storage classes are converted to IA after the validity period expires, the validity period must be set to a value greater than 30 for previous versions whose storage classes are converted to Archive.

Child node: NoncurrentDays and StorageClass

NoncurrentVersionExpiration Container No Specifies the operation on the previous versions of the object.

Child node: NoncurrentDays

ExpiredObjectDeleteMarker String No Specifies whether expired delete markers are automatically removed.
Valid values:
  • True: indicates that expired delete markers are automatically removed.
  • False: indicates that expired delete markers are not automatically removed.

Parent node: Expiration

Examples

  • Request example for an unversioned bucket
    PUT /? lifecycle HTTP/1.1
    Host: oss-example.oss.aliyuncs.com
    Content-Length: 443
    Date: Thu , 8 Jun 2017 13:08:38 GMT
    Authorization: OSS qn6qrrqxo2oawuk53otfjbyc:PYbzsdWAIWAlMW8luk*****
    <? xml version="1.0" encoding="UTF-8"? >
    <LifecycleConfiguration>
      <Rule>
        <ID>delete objects and parts after one day</ID>
        <Prefix>logs/</Prefix>
        <Status>Enabled</Status>
        <Expiration>
          <Days>1</Days>
        </Expiration>
        <AbortMultipartUpload>
          <Days>1</Days>
        </AbortMultipartUpload>
      </Rule>
      <Rule>
        <ID>transit objects to IA after 30, to Archive 60, expire after 10 years</ID>
        <Prefix>data/</Prefix>
        <Status>Enabled</Status>
        <Transition>
          <Days>30</Days>
          <StorageClass>IA</StorageClass>
        </Transition>
        <Transition>
          <Days>60</Days>
          <StorageClass>Archive</StorageClass>
        </Transition>
        <Expiration>
          <Days>3600</Days>
        </Expiration>
      </Rule>
      <Rule>
        <ID>transit objects to Archive after 60 days</ID>
        <Prefix>important/</Prefix>
        <Status>Enabled</Status>
        <Transition>
          <Days>6</Days>
          <StorageClass>Archive</StorageClass>
        </Transition>
      </Rule>
      <Rule>
        <ID>delete created before date</ID>
        <Prefix>backup/</Prefix>
        <Status>Enabled</Status>
        <Expiration>
          <CreatedBeforeDate>2017-01-01T00:00:00.000Z</CreatedBeforeDate>
        </Expiration>
        <AbortMultipartUpload>
          <CreatedBeforeDate>2017-01-01T00:00:00.000Z</CreatedBeforeDate>
        </AbortMultipartUpload>
      </Rule>
      <Rule>
        <ID>r1</ID>
        <Prefix>rule1</Prefix>
        <Tag><Key>xx</Key><Value>1</Value></Tag>
        <Tag><Key>yy</Key><Value>2</Value></Tag>
        <Status>Enabled</Status>
        <Expiration>
          <Days>30</Days>
        </Expiration>
      </Rule>
      <Rule>
        <ID>r2</ID>
        <Prefix>rule2</Prefix>
        <Tag><Key>xx</Key><Value>1</Value></Tag>
        <Status>Enabled</Status>
        <Transition>
          <Days>60</Days>
        <StorageClass>Archive</StorageClass>
        </Transition>
      </Rule>
    </LifecycleConfiguration>            
    Sample response
    HTTP/1.1 200 OK
    x-oss-request-id: 534B371674A4D890*****
    Date: Thu , 8 Jun 2017 13:08:38 GMT
    Content-Length: 0
    Connection: keep-alive
    Server: AliyunOSS
  • Sample request for a versioning-enabled bucket
    PUT /? lifecycle HTTP/1.1
    Host: oss-example.oss.aliyuncs.com
    Content-Length: 336
    Date: Mon , 6 May 2019 15:23:20 GMT
    Authorization: OSSWnjl3fg9fdv8fg4b8sdf:Phuu8bBhS8dsff2a*****
    <? xml version="1.0" encoding="UTF-8"? >
    <LifecycleConfiguration>
      <Rule>
        <ID>delete example</ID>
        <Prefix>logs/</Prefix>
        <Status>Enabled</Status>
        <Expiration>
          <ExpiredObjectDeleteMarker>true</ExpiredObjectDeleteMarker>     
        </Expiration>
        <NoncurrentVersionExpiration>
          <NoncurrentDays>5</NoncurrentDays>
        </NoncurrentVersionExpiration>
        <AbortMultipartUpload>
          <Days>1</Days>
        </AbortMultipartUpload>
      </Rule>
      <Rule>
        <ID>transit example</ID>
        <Prefix>data/</Prefix>
        <Status>Enabled</Status>
        <Transition>
          <Days>30</Days>
          <StorageClass>IA</StorageClass>
        </Transition>
        <NoncurrentVersionTransition>
          <NoncurrentDays>10</NoncurrentDays>
          <StorageClass>IA</StorageClass>
        </NoncurrentVersionTransition>
      </Rule>
    </LifecycleConfiguration>
    Sample response
    HTTP/1.1 200 OK
    x-oss-request-id: 7D3435J59A9812B*****
    Date: Mon , 6 May 2019 15:23:20 GMT
    Content-Length: 0
    Connection: keep-alive
    Server: AliyunOSS

SDK

The SDKs of the PutBucketLifecycle operation for various programming languages are as follows:

Error codes

Error code HTTP status code Description
AccessDenied 403 The error message returned because you are not authorized to perform this operation. Only the owner of a bucket can initiate PutBucketLifecycle requests for the bucket.
InvalidArgument 400
  • The storage class of objects in a Standard bucket can be converted from Standard to IA or Archive. You can configure multiple rules to convert a Standard object to an IA or Archive object. Note that the validity period for conversion to Archive must be longer than that for conversion to IA.
  • The validity period specified for Expiration must be longer than that specified for Transition. Likewise, the expiration time specified for Expiration must be later than that specified for Transition.