# Bucket Lifecycle

The **lifecycle configuration** allow you to set an expiration policy on your objects and auto-delete them.

For example, you may need for some objects to be deleted automatically.

In this example, we will automatically create a policy to delete objects with a key starting with `reports/` after 90 days. We could use a **GUI Client** or **AWS SDK**, but we will use **AWS CLI** to do so.

## Limitations

* Lifecycle is a bucket level concept.
* Maximum of 1000 lifecycle rules per bucket is applicable.
* There may be a delay between the expiration date and the date at which **Object Storage Service** removes an object.
* Always round up the resulting time to the next day midnight UTC.

{% hint style="warning" %}
Deleted Object cannot be restored.
{% endhint %}

## Manage lifecycle policy <a href="#lifecycleconfiguration-managelifecyclepolicy" id="lifecycleconfiguration-managelifecyclepolicy"></a>

Bucket lifecycle configuration can be managed using **aws s3api** (other tools or SDK works too)**:**

* put-bucket-lifecycle
* get-bucket-lifecycle
* delete-bucket-lifecycle

{% hint style="info" %}
We use **aws s3** and \*\*aws `s3api`\*\*command line tools from AWSCLIv2 on Linux.

`${S3_ENDPOINT}` & `${S3_PROFILE}` are environment variables.
{% endhint %}

### Create a lifecycle policy <a href="#lifecycleconfiguration-createalifecylepolicy" id="lifecycleconfiguration-createalifecylepolicy"></a>

Create JSON file and put your policy in it:

{% code title="delete\_after\_3\_days.json" lineNumbers="true" %}

```json
{
  "Rules": [
    {
      "Filter": {
        "Prefix": ""
      },
      "Expiration": {
        "Days": 3
      },
      "Status": "Enabled",
      "ID": "Delete After 3 days."
    }
  ]
}
```

{% endcode %}

Apply it to the bucket: **bucket-test**

{% code overflow="wrap" %}

```bash
aws s3api --endpoint-url=${S3_ENDPOINT} put-bucket-lifecycle --bucket bucket-test --lifecycle-configuration file://delete_after_3days.json --profile ${S3_PROFILE}
```

{% endcode %}

### Get a lifecycle configuration <a href="#lifecycleconfiguration-getalifecyleconfiguration" id="lifecycleconfiguration-getalifecyleconfiguration"></a>

{% code overflow="wrap" %}

```bash
aws s3api --endpoint-url=${S3_ENDPOINT} get-bucket-lifecycle --bucket bucket-test --profile ${S3_PROFILE}
```

{% endcode %}

### Delete a lifecycle configuration <a href="#lifecycleconfiguration-deletealifecyleconfiguration" id="lifecycleconfiguration-deletealifecyleconfiguration"></a>

{% code overflow="wrap" %}

```bash
aws s3api --endpoint-url=${S3_ENDPOINT} delete-bucket-lifecycle --bucket bucket-test --profile ${S3_PROFILE}
```

{% endcode %}

## Supported lifecycle configuration elements <a href="#lifecycleconfiguration-supportedlifecycleconfigurationelements" id="lifecycleconfiguration-supportedlifecycleconfigurationelements"></a>

<table data-full-width="true"><thead><tr><th width="278">Name</th><th width="383.33333333333337">Description</th><th>Required</th></tr></thead><tbody><tr><td><strong><code>And</code></strong></td><td><ul><li>Container for specify rule filters. These filters determine the subset of objects to which the rule applies.</li><li>Type: String</li><li>Ancestor: <strong><code>Rule</code></strong></li></ul></td><td>Yes, if you specify more than one filter condition (for example, one prefix and one or more tags).</td></tr><tr><td><strong><code>Date</code></strong></td><td><ul><li>Date when you want S3 to take the action.</li><li>The date value must conform to the ISO 8601 format. The time is always midnight UTC.</li><li>Type: String</li><li>Ancestor: <strong><code>Expiration</code></strong></li></ul></td><td>Yes, if <code>Days</code> and <strong><code>ExpiredObjectDeleteMarker</code></strong> are absent.</td></tr><tr><td><strong><code>Days</code></strong></td><td><ul><li>Specifies the number of days after object creation when the specific rule action takes effect.</li><li>Type: Nonnegative Integer when used with Transition, Positive Integer when used with Expiration.</li><li>Ancestor: <strong><code>Expiration</code></strong></li></ul></td><td>Yes, if <code>Date</code> and <strong><code>ExpiredObjectDeleteMarker</code></strong> are absent.</td></tr><tr><td><strong><code>Expiration</code></strong></td><td><ul><li>This action specifies a period in an object's lifetime when S3 should take the appropriate expiration action. The action S3 takes depends on whether the bucket is versioning-enabled.</li><li>If versioning has never been enabled on the bucket, S3 deletes the only copy of the object permanently. Otherwise, if your bucket is versioning-enabled (or versioning is suspended), the action applies only to the current version of the object. A versioning-enabled bucket can have many versions of the same object, one current version, and zero or more non-current versions.</li><li>Instead of deleting the current version, S3 makes it a non-current version by adding a delete marker as the new current version.</li></ul><p><strong>Note:</strong></p><hr><ul><li>If your bucket state is versioning-suspended, S3 creates a delete marker with version ID null. If you have a version with version ID null, then S3 overwrites that version.</li><li>To set expiration for non-current objects, you must use the <code>NoncurrentVersionExpiration</code> action.</li><li>Type: Container</li><li>Children: <strong><code>Days</code></strong> or <strong><code>Date</code></strong></li><li>Ancestor: <strong><code>Rule</code></strong></li></ul></td><td>Yes, if no other action is present in the Rule.</td></tr><tr><td><strong><code>Filter</code></strong></td><td><ul><li>Container for elements that describe the filter identifying a subset of objects to which the lifecycle rule applies. If you specify an empty filter (<strong><code>"Prefix": {}</code></strong>), the rule applies to all objects in the bucket.</li><li>Type: String</li><li>Children: <strong><code>Prefix</code></strong>, <strong><code>Tag</code></strong></li><li>Ancestor: <strong><code>Rule</code></strong></li></ul></td><td>Yes</td></tr><tr><td><strong><code>ID</code></strong></td><td><ul><li>Unique identifier for the rule. The value cannot be longer than 255 characters.</li><li>Type: String</li><li>Ancestor: <strong><code>Rule</code></strong></li></ul></td><td>No</td></tr><tr><td><strong><code>Key</code></strong></td><td><ul><li>Specifies the key of a tag. A tag key can be up to 128 Unicode characters in length.</li><li>Tag keys that you specify in a lifecycle rule filter must be unique.</li><li>Type: String</li><li>Ancestor: <strong><code>Tag</code></strong></li></ul></td><td>Yes, if <code>&#x3C;</code><strong><code>Tag</code></strong><code>></code> parent is specified.</td></tr><tr><td><strong><code>LifecycleConfiguration</code></strong></td><td><ul><li>Container for lifecycle rules. You can add as many as 1,000 rules.</li><li>Type: Container</li><li>Children: <strong><code>Rule</code></strong></li><li>Ancestor: None</li></ul></td><td>Yes</td></tr><tr><td><strong><code>ExpiredObjectDeleteMarker</code></strong></td><td><ul><li>On a versioned bucket (versioning-enabled or versioning-suspended bucket), you can add this element in the lifecycle configuration to direct S3 to delete expired object delete markers. On a non-versioned bucket, adding this element in a policy is meaningless because you cannot have delete markers and the element does not do anything.</li><li>When you specify this lifecycle action, the rule cannot specify a tag-based filter.</li><li>Type: String</li><li>Valid values: true | false (the value false is allowed, but it is no-op and S3 does not take action if the value is false)</li><li>Ancestor: <strong><code>Expiration</code></strong>.</li></ul></td><td>Yes, if <strong><code>Date</code></strong> and <strong><code>Days</code></strong> are absent.<br></td></tr><tr><td><strong><code>NoncurrentDays</code></strong></td><td><ul><li>Specifies the number of days an object is non-current before S3 can perform the associated action.</li><li>Type: Positive Integer when used with <strong><code>NoncurrentVersionExpiration</code></strong>.</li><li>Ancestor: <strong><code>NoncurrentVersionExpiration</code></strong></li></ul></td><td>Yes</td></tr><tr><td><strong><code>NoncurrentVersionExpiration</code></strong></td><td><ul><li>Specifies when non-current object versions expire. Upon expiration, S3 permanently deletes the non-current object versions.</li><li>You set this lifecycle configuration action on a bucket that has versioning enabled (or suspended) to request that S3 delete non-current object versions at a specific period in the object's lifetime.</li><li>Type: Container</li><li>Children: <strong><code>NoncurrentDays</code></strong></li><li>Ancestor: <strong><code>Rule</code></strong></li></ul></td><td>Yes, if no other action is present in the <strong><code>Rule</code></strong>.</td></tr><tr><td><strong><code>Prefix</code></strong></td><td><ul><li>Object key prefix identifying one or more objects to which the rule applies. Empty prefix (&#x3C;Prefix>&#x3C;/Prefix>) indicates there is no filter based on key prefix.</li></ul><p><strong>Note:</strong></p><hr><p>Supports &#x3C;Prefix> with and without &#x3C;Filter>. (Deprecated)</p><p>PUT Bucket lifecycle with &#x3C;Filter></p><ul><li>There can be at most one Prefix in a lifecycle rule Filter.</li><li>Type: String</li><li>Ancestor: <strong><code>Filter</code></strong> or <strong><code>And</code></strong> (if you specify multiple filters such as a prefix and one or more tags)</li></ul></td><td>No</td></tr><tr><td></td><td></td><td></td></tr><tr><td><code>"Prefix": "", # No Prefix "Prefix": "documents/",</code></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td><code>"Filter": { "Prefix": ""}"Filter": { "Prefix": "documents/"}</code></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td><code>"Prefix": "", # No Prefix "Prefix": "documents/",</code></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td><code>"Filter": { "Prefix": ""}"Filter": { "Prefix": "documents/"}</code></td><td></td><td></td></tr><tr><td><strong><code>Rule</code></strong></td><td><ul><li>Container for a lifecycle rule. A lifecycle configuration can contain as many as 1,000 rules.</li><li>Type: Container</li><li>Ancestor: <strong><code>LifecycleConfiguration</code></strong></li></ul></td><td>Yes</td></tr><tr><td><strong><code>Status</code></strong></td><td><ul><li>if Enabled, S3 executes the rule as scheduled. If Disabled, S3 ignores the rule.</li><li>Type: String</li><li>Ancestor: <strong><code>Rule</code></strong></li><li>Valid values: Enabled, Disabled.</li></ul></td><td>Yes</td></tr><tr><td><strong><code>Value</code></strong></td><td><ul><li>Specifies the value for a tag key. Each object tag is a key-value pair.</li><li>Tag value can be up to 256 Unicode characters in length.</li><li>Type: String</li><li>Ancestor: <strong><code>Tag</code></strong></li></ul></td><td>Yes, if <code>&#x3C;</code><strong><code>Tag</code></strong><code>></code> parent is specified.</td></tr></tbody></table>
