Lifecycle configuration elements
A S3 Lifecycle configuration consist of Lifecycle rules that include various elements that describe the actions Amazon S3 takes during and objects lifetime. You specify an Amazon S3 Lifecycle configuration as XML, consisting of one or more Lifecycle rules, where each rule consists of one or more elements.
<LifecycleConfiguration> <Rule> <Element> </Rule> <Rule> <Element> <Element> </Rule> </LifecycleConfiguration>
Each rule consists of the following:
-
Rule metadata that includes a rule ID, and a status that indicates whether the rule is enabled or disabled. If a rule is disabled, Amazon S3 doesn't perform any actions specified in the rule.
-
A filter that identifies the objects to which the rule applies. You can specify a filter by using the object size, the object key prefix, one or more object tags, or a combination of filters.
-
One or more transition or expiration actions with a date or a time period in the object's lifetime when you want Amazon S3 to perform the specified action.
Topics
The following sections describe the XML elements in an S3 Lifecycle configuration. For example configurations, see Examples of S3 Lifecycle configurations.
ID element
An S3 Lifecycle configuration can have up to 1,000 rules. This limit is not adjustable.
The <ID>
element uniquely identifies a rule. ID length is limited to
255 characters.
Status element
The <Status>
element value can be either Enabled
or
Disabled
. If a rule is disabled, Amazon S3 doesn't perform any of the
actions defined in the rule.
Filter element
A S3 Lifecycle rule can apply to all or a subset of objects in a bucket based on the
<Filter>
element that you specify in the rule.
You can filter objects by key prefix, object tags, or a combination of both (in which
case Amazon S3 uses a logical AND
to combine the filters). For examples and more information about filters see, Adding filters to Lifecycle rules.
-
Specifying a filter by using key prefixes – This example shows an S3 Lifecycle rule that applies to a subset of objects based on the key name prefix (
logs/
). For example, the Lifecycle rule applies to the objectslogs/mylog.txt
,logs/temp1.txt
, andlogs/test.txt
. The rule does not apply to the objectexample.jpg
.<LifecycleConfiguration> <Rule> <Filter> <Prefix>logs/</Prefix> </Filter>
transition/expiration actions
... </Rule> ... </LifecycleConfiguration>If you want to apply a lifecycle action to a subset of objects based on different key name prefixes, specify separate rules. In each rule, specify a prefix-based filter. For example, to describe a lifecycle action for objects with the key prefixes
projectA/
andprojectB/
, you specify two rules as follows:<LifecycleConfiguration> <Rule> <Filter> <Prefix>projectA/</Prefix> </Filter>
transition/expiration actions
... </Rule> <Rule> <Filter> <Prefix>projectB/</Prefix> </Filter>transition/expiration actions
... </Rule> </LifecycleConfiguration>For more information about object keys, see Naming Amazon S3 objects.
-
Specifying a filter based on object tags – In the following example, the Lifecycle rule specifies a filter based on a tag (
) and value (key
). The rule then applies only to a subset of objects with the specific tag.value
<LifecycleConfiguration> <Rule> <Filter> <Tag> <Key>
key
</Key> <Value>value
</Value> </Tag> </Filter>transition/expiration actions
... </Rule> </LifecycleConfiguration>You can specify a filter based on multiple tags. You must wrap the tags in the
<And>
element, as shown in the following example. The rule directs Amazon S3 to perform lifecycle actions on objects with two tags (with the specific tag key and value).<LifecycleConfiguration> <Rule> <Filter> <And> <Tag> <Key>
key1
</Key> <Value>value1
</Value> </Tag> <Tag> <Key>key2
</Key> <Value>value2
</Value> </Tag> ... </And> </Filter>transition/expiration actions
</Rule> </Lifecycle>The Lifecycle rule applies to objects that have both of the tags specified. Amazon S3 performs a logical
AND
. Note the following:-
Each tag must match both the key and value exactly. If you specify only a
<Key>
element and no<Value>
element, the rule will apply only to objects that match the tag key and that do not have a value specified. -
The rule applies to a subset of objects that has all the tags specified in the rule. If an object has additional tags specified, the rule will still apply.
Note
When you specify multiple tags in a filter, each tag key must be unique.
-
-
Specifying a filter based on both the prefix and one or more tags – In a Lifecycle rule, you can specify a filter based on both the key prefix and one or more tags. Again, you must wrap all of these filter elements in the
<And>
element, as follows:<LifecycleConfiguration> <Rule> <Filter> <And> <Prefix>
key-prefix
</Prefix> <Tag> <Key>key1
</Key> <Value>value1
</Value> </Tag> <Tag> <Key>key2
</Key> <Value>value2
</Value> </Tag> ... </And> </Filter> <Status>Enabled</Status>transition/expiration actions
</Rule> </LifecycleConfiguration>Amazon S3 combines these filters by using a logical
AND
. That is, the rule applies to the subset of objects with the specified key prefix and the specified tags. A filter can have only one prefix, and zero or more tags. -
You can specify an empty filter, in which case the rule applies to all objects in the bucket.
<LifecycleConfiguration> <Rule> <Filter> </Filter> <Status>Enabled</Status>
transition/expiration actions
</Rule> </LifecycleConfiguration> -
To filter a rule by object size, you can specify a minimum size (
ObjectSizeGreaterThan
) or a maximum size (ObjectSizeLessThan
), or you can specify a range of object sizes.Object size values are in bytes. By default, objects smaller than 128 KB will not be transitioned to any storage class, unless you specify a smaller minimum size (
ObjectSizeGreaterThan
) or a maximum size (ObjectSizeLessThan
). For more information, see Example: Allowing objects smaller than 128 KB to be transitioned.<LifecycleConfiguration> <Rule> <Filter> <ObjectSizeGreaterThan>500</ObjectSizeGreaterThan> </Filter> <Status>Enabled</Status>
transition/expiration actions
</Rule> </LifecycleConfiguration>Note
The
ObjectSizeGreaterThan
andObjectSizeLessThan
filters exclude the specified values. For example, if you set objects sized 128 KB to 1024 KB to move from the S3 Standard storage class to the S3 Standard-IA storage class, objects that are exactly 1024 KB and 128 KB won't transition to S3 Standard-IA. Instead, the rule will apply only to objects that are greater than 128 KB and less than 1024 KB in size.If you're specifying an object size range, the
ObjectSizeGreaterThan
integer must be less than theObjectSizeLessThan
value. When using more than one filter, you must wrap the filters in an<And>
element. The following example shows how to specify objects in a range between 500 bytes and 64,000 bytes.<LifecycleConfiguration> <Rule> <Filter> <And> <Prefix>
key-prefix
</Prefix> <ObjectSizeGreaterThan>500</ObjectSizeGreaterThan> <ObjectSizeLessThan>64000</ObjectSizeLessThan> </And> </Filter> <Status>Enabled</Status>transition/expiration actions
</Rule> </LifecycleConfiguration>
Elements to describe lifecycle actions
You can direct Amazon S3 to perform specific actions in an object's lifetime by specifying one or more of the following predefined actions in an S3 Lifecycle rule. The effect of these actions depends on the versioning state of your bucket.
-
Transition
action element – You specify theTransition
action to transition objects from one storage class to another. For more information about transitioning objects, see Supported transitions. When a specified date or time period in the object's lifetime is reached, Amazon S3 performs the transition.For a versioned bucket (versioning-enabled or versioning-suspended bucket), the
Transition
action applies to the current object version. To manage noncurrent versions, Amazon S3 defines theNoncurrentVersionTransition
action (described later in this topic).
-
Expiration
action element – TheExpiration
action expires objects identified in the rule and applies to eligible objects in any of the Amazon S3 storage classes. For more information about storage classes, see Understanding and managing Amazon S3 storage classes. Amazon S3 makes all expired objects unavailable. Whether the objects are permanently removed depends on the versioning state of the bucket.-
Nonversioned bucket – The
Expiration
action results in Amazon S3 permanently removing the object. -
Versioned bucket – For a versioned bucket (that is, versioning-enabled or versioning-suspended), there are several considerations that guide how Amazon S3 handles the
Expiration
action. For versioning-enabled or versioning-suspended buckets, the following applies:-
The
Expiration
action applies only to the current version (it has no impact on noncurrent object versions). -
Amazon S3 doesn't take any action if there are one or more object versions and the delete marker is the current version.
-
If the current object version is the only object version and it is also a delete marker (also referred as an expired object delete marker, where all object versions are deleted and you only have a delete marker remaining), Amazon S3 removes the expired object delete marker. You can also use the expiration action to direct Amazon S3 to remove any expired object delete markers. For an example, see Removing expired object delete markers in a versioning-enabled bucket.
For more information, see Retaining multiple versions of objects with S3 Versioning.
Also consider the following when setting up Amazon S3 to manage expiration:
-
Versioning-enabled bucket
If the current object version is not a delete marker, Amazon S3 adds a delete marker with a unique version ID. This makes the current version noncurrent, and the delete marker the current version.
-
Versioning-suspended bucket
In a versioning-suspended bucket, the expiration action causes Amazon S3 to create a delete marker with
null
as the version ID. This delete marker replaces any object version with a null version ID in the version hierarchy, which effectively deletes the object.
-
-
In addition, Amazon S3 provides the following actions that you can use to manage noncurrent object versions in a versioned bucket (that is, versioning-enabled and versioning-suspended buckets).
-
NoncurrentVersionTransition
action element – Use this action to specify when Amazon S3 transitions objects to the specified storage class. You can base this expiration on a certain number of days since the objects became noncurrent. In addition to the number of days, you can also provide a maximum number of noncurrent versions to retain (between 1 and 100). This value determines how many newer noncurrent versions must exist before Amazon S3 can perform the associated action on a given version. Amazon S3 will transition any additional noncurrent versions beyond the specified number to retain.To specify the maximum number of noncurrent versions, you must also provide a
Filter
element. If you don't specify aFilter
element, Amazon S3 generates anInvalidRequest
error when you provide a maximum number of noncurrent versions.For more information about transitioning objects, see Supported transitions. For details about how Amazon S3 calculates the date when you specify the number of days in the
NoncurrentVersionTransition
action, see Lifecycle rules: Based on an object's age. -
NoncurrentVersionExpiration
action element – Use this action to direct Amazon S3 to permanently delete noncurrent versions of objects. These deleted objects can't be recovered. You can base this expiration on a certain number of days since the objects became noncurrent. In addition to the number of days, you can also provide a maximum number of noncurrent versions to retain (between 1 and 100). This value specifies how many newer noncurrent versions must exist before Amazon S3 can perform the associated action on a given version. Amazon S3 will permanently delete any additional noncurrent versions beyond the specified number to retain.To specify the maximum number of noncurrent versions, you must also provide a
Filter
element. If you don't specify aFilter
element, Amazon S3 generates anInvalidRequest
error when you provide a maximum number of noncurrent versions.Delayed removal of noncurrent objects can be helpful when you need to correct any accidental deletes or overwrites. For example, you can configure an expiration rule to delete noncurrent versions five days after they become noncurrent. For example, suppose that on 1/1/2014 at 10:30 AM UTC, you create an object called
photo.gif
(version ID 111111). On 1/2/2014 at 11:30 AM UTC, you accidentally deletephoto.gif
(version ID 111111), which creates a delete marker with a new version ID (such as version ID 4857693). You now have five days to recover the original version ofphoto.gif
(version ID 111111) before the deletion is permanent. On 1/8/2014 at 00:00 UTC, the Lifecycle rule for expiration runs and permanently deletesphoto.gif
(version ID 111111), five days after it became a noncurrent version.For details about how Amazon S3 calculates the date when you specify the number of days in an
NoncurrentVersionExpiration
action, see Lifecycle rules: Based on an object's age.Note
Object expiration lifecycle configurations don't remove incomplete multipart uploads. To remove incomplete multipart uploads, you must use the
AbortIncompleteMultipartUpload
Lifecycle configuration action that's described later in this section.
In addition to the transition and expiration actions, you can use the following Lifecycle configuration actions to direct Amazon S3 to stop incomplete multipart uploads or to remove expired object delete markers:
-
AbortIncompleteMultipartUpload
action element – Use this element to set a maximum time (in days) that you want to allow multipart uploads to remain in progress. If the applicable multipart uploads (determined by the key nameprefix
specified in the Lifecycle rule) aren't successfully completed within the predefined time period, Amazon S3 stops the incomplete multipart uploads. For more information, see Aborting a multipart upload.Note
You can't specify this lifecycle action in a rule that has a filter that uses object tags.
-
ExpiredObjectDeleteMarker
action element – In a versioning-enabled bucket, a delete marker with zero noncurrent versions is referred to as an expired object delete marker. You can use this lifecycle action to direct Amazon S3 to remove expired object delete markers. For an example, see Removing expired object delete markers in a versioning-enabled bucket.Note
You can't specify this lifecycle action in a rule that has a filter that uses object tags.
How Amazon S3 calculates how long an object has been noncurrent
In a versioning-enabled bucket, you can have multiple versions of an object. There is always one current version, and zero or more noncurrent versions. Each time you upload an object, the current version is retained as the noncurrent version and the newly added version, the successor, becomes the current version. To determine the number of days an object is noncurrent, Amazon S3 looks at when its successor was created. Amazon S3 uses the number of days since its successor was created as the number of days an object is noncurrent.
Restoring previous versions of an object when using S3 Lifecycle configurations
As explained in Restoring previous versions, you can use either of the following two methods to retrieve previous versions of an object:
-
Method 1 – Copy a noncurrent version of the object into the same bucket. The copied object becomes the current version of that object, and all object versions are preserved.
-
Method 2 – Permanently delete the current version of the object. When you delete the current object version, you, in effect, turn the noncurrent version into the current version of that object.
When you're using S3 Lifecycle configuration rules with versioning-enabled buckets, we recommend as a best practice that you use Method 1.
S3 Lifecycle operates under an eventually consistent model. A current version that you permanently deleted might not disappear until the changes propagate to all of the Amazon S3 systems. (Therefore, Amazon S3 might be temporarily unaware of this deletion.) In the meantime, the lifecycle rule that you configured to expire noncurrent objects might permanently remove noncurrent objects, including the one that you want to restore. So, copying the old version, as recommended in Method 1, is the safer alternative.
Lifecycle actions and bucket versioning state
The following table summarizes the behavior of the S3 Lifecycle configuration rule actions on objects in relation to the versioning state of the bucket that contains the object.
Action | Nonversioned bucket (versioning not enabled) | Versioning-enabled bucket | Versioning-suspended bucket |
---|---|---|---|
When a specified date or time period in the object's lifetime is reached. |
Amazon S3 transitions the object to the specified storage class. | Amazon S3 transitions the current version of the object to the specified storage class. | Same behavior as a versioning-enabled bucket. |
When a specified date or time period in the object's lifetime is reached. |
The Expiration action deletes the object, and the
deleted object can't be recovered. |
If the current version isn't a delete marker, Amazon S3 creates a delete marker, which becomes the current version, and the existing current version is retained as a noncurrent version. | The lifecycle action creates a delete marker with
null version ID, which becomes the current version.
If the version ID of the current version of the object is
null , the Expiration action
permanently deletes this version. Otherwise, the current version is
retained as a noncurrent version. |
When the object has been classified as noncurrent for the specified number of days or the specified maximum number of noncurrent versions to retain has been exceeded. |
NoncurrentVersionTransition has no effect. |
Amazon S3 transitions the noncurrent object versions to the specified storage class. |
Same behavior as a versioning-enabled bucket. |
When the object has been classified as noncurrent for the specified number of days or the specified maximum number of noncurrent versions to retain has been exceeded. |
NoncurrentVersionExpiration has no effect. |
The NoncurrentVersionExpiration action permanently
deletes the noncurrent version of the object, and the deleted object
can't be recovered. |
Same behavior as a versioning-enabled bucket. |
Lifecycle rules: Based on an object's age
You can specify a time period, in the number of days from the creation (or modification) of the object, when Amazon S3 can take the specified action.
When you specify the number of days in the Transition
and
Expiration
actions in an S3 Lifecycle configuration, note the
following:
-
The value that you specify is the number of days since object creation when the action will occur.
-
Amazon S3 calculates the time by adding the number of days specified in the rule to the object creation time and rounding the resulting time to the next day at midnight UTC. For example, if an object was created on 1/15/2014 at 10:30 AM UTC and you specify 3 days in a transition rule, then the transition date of the object would be calculated as 1/19/2014 00:00 UTC.
Note
Amazon S3 maintains only the last modified date for each object. For example, the Amazon S3 console shows the Last modified date in the object's Properties pane. When you initially create a new object, this date reflects the date that the object is created. If you replace the object, the date changes accordingly. Therefore, the creation date is synonymous with the Last modified date.
When specifying the number of days in the NoncurrentVersionTransition
and NoncurrentVersionExpiration
actions in a Lifecycle configuration,
note the following:
-
The value that you specify is the number of days from when the version of the object becomes noncurrent (that is, when the object is overwritten or deleted) that Amazon S3 will perform the action on the specified object or objects.
-
Amazon S3 calculates the time by adding the number of days specified in the rule to the time when the new successor version of the object is created and rounding the resulting time to the next day at midnight UTC. For example, in your bucket, suppose that you have a current version of an object that was created on 1/1/2014 at 10:30 AM UTC. If the new version of the object that replaces the current version is created on 1/15/2014 at 10:30 AM UTC, and you specify 3 days in a transition rule, the transition date of the object is calculated as 1/19/2014 00:00 UTC.
Lifecycle rules: Based on a specific date
When specifying an action in an S3 Lifecycle rule, you can specify a date when you want Amazon S3 to take the action. When the specific date arrives, Amazon S3 applies the action to all qualified objects (based on the filter criteria).
If you specify an S3 Lifecycle action with a date that is in the past, all qualified objects become immediately eligible for that lifecycle action.
Important
The date-based action is not a one-time action. Amazon S3 continues to apply the
date-based action even after the date has passed, as long as the rule status is
Enabled
.
For example, suppose that you specify a date-based Expiration
action to delete all objects (assume that no filter is specified in the rule).
On the specified date, Amazon S3 expires all the objects in the bucket. Amazon S3 also
continues to expire any new objects that you create in the bucket. To stop the
lifecycle action, you must either remove the action from the lifecycle rule,
disable the rule, or delete the rule from the lifecycle configuration.
The date value must conform to the ISO 8601 format. The time is always midnight UTC.
Note
You can't create date-based Lifecycle rules by using the Amazon S3 console, but you can view, disable, or delete such rules.