You can call this operation to configure lifecycle rules for a bucket. After you configure a lifecycle rule for a bucket, OSS automatically deletes the objects that match the rule or convert the storage class of the objects at the time specified in the lifecycle rule.

Usage notes

When you call the PutBucketLifecycle operation, note the following items:

  • Only the owner of a bucket can initiate PutBucketLifecycle requests to the bucket.
  • If no lifecycle rules are configured for a bucket, a lifecycle rule is created after you call PutBucketLifecycle. If a lifecycle rule is configured for the bucket, a lifecycle rule is created and the existing lifecycle rule is overwritten.
  • PutBucketLifecycle uses the overwriting semantics. New lifecycle rules overwrite the existing rules. To add lifecycle rules for a bucket, call GetBucketLifecycle to obtain the existing lifecycle configurations of the bucket, add new lifecycle configurations, and call PutBucketLifecycle to update the lifecycle configurations for the bucket.
  • When you call the PutBucketLifecycle operation, you can set a validity period or expiration date 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 The date based on which to run lifecycle rules. OSS performs the specified operation on data whose last modified date is before this date. Specify the time in the ISO 8601 standard. The time must be at UTC 00:00:00. For example, 2002-10-11T00:00:00.000Z indicates that objects last modified before 2002-10-11T00:00:00.000Z are deleted or the storage class of these objects is converted to another storage class. Objects last modified at or after this time are not deleted or their storage class is not converted to another storage class.

Parent node: Expiration or AbortMultipartUpload

Days Positive integer Either Days or CreatedBeforeDate The number of days within which objects can be retained after they are last modified.

Parent node: Expiration

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

Child node: Days, CreatedBeforeDate, or ExpiredObjectDeleteMarker

Parent node: Rule

AbortMultipartUpload Container No The delete operation on the multipart upload tasks that are incomplete.

Child node: Days or CreatedBeforeDate

Parent node: Rule.

ID String No The unique ID of a lifecycle rule. The ID can be a maximum of 255 bytes in length. 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 The container that stores lifecycle configurations. The container can contain up to 1,000 rules.

Chile node: Rule

Parent node: none

Prefix String Yes The prefix of names of objects to which the rule applies. The prefixes specified by different rules cannot overlap.
  • If Prefix is specified, this rule applies only to objects whose names contain the specified prefix in the bucket.
  • If Prefix is not specified, this rule applies to all objects in the bucket.

Child node: none

Parent node: Rule

Rule Container Yes Identifies the rule.
  • 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 nodes: ID, Prefix, Status, and Expiration

Parent node: LifecycleConfiguration

Status String Yes Specifies whether to run the rule. A value of Enabled indicates that OSS runs the rule. A 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 The storage class of objects after conversion.
Note You can convert the storage class of objects in an IA bucket to Archive or Cold Archive, but not Standard.

Valid values: IA, Archive, and ColdArchive

Parent node: Transition

Transition Container No The validity period within which objects can be retained and the conversion operation on the objects. After the validity period or expiration date, the storage class of the objects is converted to IA, Archive, or Cold Archive.
Note The storage class of Standard objects can be converted to IA, Archive, or Cold 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 class is converted to IA after the validity period, the validity period must be set to a value greater than 30 for objects whose storage class is converted to Archive.

Parent node: Rule

Child nodes: Days, CreatedBeforeDate, and StorageClass
Note Either Days or CreatedBeforeDate
Tag Container No The object tags.

Parent node: Rule

Child nodes: Key and Value

Key String Yes if Tag is configured The tag key.

Parent node: Tag

Value String Yes if Tag is configured The tag value.

Parent node: Tag

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

Parent nodes: NoncurrentVersionTransition and NoncurrentVersionExpiration

NoncurrentVersionTransition Container No The validity period within which previous versions can be retained and the conversion operation on the previous versions. After the validity period, 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 class is converted to IA after the validity period, the validity period must be set to a value greater than 30 for previous versions whose storage class is converted to Archive.

Child nodes: NoncurrentDays and StorageClass

NoncurrentVersionExpiration Container No The delete operation on the previous versions of the object.

Child node: NoncurrentDays

ExpiredObjectDeleteMarker String No Specifies whether to automatically remove expired delete markers.
Valid values: true and false
  • true: Expired delete markers are automatically removed.
  • false: Expired delete markers are not automatically removed.

Parent node: Expiration

Examples

  • Sample request 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 versioned 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

You can use SDKs for the following programming languages to call InitiateMultipartUpload:

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 to the bucket.
InvalidArgument 400
  • The storage class of objects in a Standard bucket can be converted from Standard to IA or Archive. When you configure lifecycle rules, you can configure conversion to Standard and conversion to IA at the same time. 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.