S3 Lifecycle Rules – Delete expired objects

TOC

S3 Lifecycle Rules – Delete expired objects

One of the features provided by S3 is lifecycle rules.
There are two types of lifecycle rules.
One is to migrate the storage class in which an object is stored to another one.
The other is to delete objects that have expired.

Expiration actions – These actions define when objects expire. Amazon S3 deletes expired objects on your behalf.

Managing your storage lifecycle

In this article, we will see how to delete this expired object.

Environment

Diagram of S3 Lifecycle Rules - Delete expired objects

Create an S3 bucket.

Set up the bucket lifecycle rules.
This rule sets the expiration date of the object and automatically deletes it.

CloudFormation template files

Build the above configuration with CloudFormation.
The CloudFormation templates are located at the following URL

https://github.com/awstut-an-r/awstut-fa/tree/main/108

Explanation of key points of the template files

S3 bucket

Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: Private
      BucketName: !Ref Prefix
      LifecycleConfiguration:
        Rules:
          - ExpirationInDays: !Ref ExpirationInDays
            Id: !Sub "${Prefix}-Lifecyclerule"
            Status: Enabled
Code language: YAML (yaml)

Lifecycle rules are set with the LifecycleConfiguration property.

The ExpirationInDays property allows you to set the object’s expiration date.
In this case, as this is a verification, specify “1” for this property.
This will cause the object to be deleted one day after it is placed.

Architecting

Using CloudFormation, we will build this environment and verify the actual behavior.

Create CloudFormation stacks and check resources in stacks

Create CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following page

あわせて読みたい
CloudFormation’s nested stack 【How to build an environment with a nested CloudFormation stack】 Examine nested stacks in CloudFormation. CloudFormation allows you to nest stacks. Nested ...

After checking the resources in each stack, information on the main resources created this time is as follows

  • S3 bucket: fa-108

Confirm each resource from AWS Management Console.

Check the S3 bucket.

Detail of S3 1.

Sure enough, a lifecycle rule has been created.

Check the details of the rule.

Detail of S3 2.

You can see that the rule is set to automatically delete the object one day after it is uploaded.

Checking Action

Now that we are ready, we place the object in this bucket.

Detail of S3 3.

Check the details of the placed object.

Detail of S3 4.

Indeed, the object has an expiration date.
It says “December 26, 2022, 09:00:00”.

Wait until this date and time.

Detail of S3 5.

Sure enough, the object is deleted.

By setting a lifecycle rule in this way, expired objects can be deleted automatically.

Summary

We have confirmed how to delete expired objects using S3 bucket lifecycle rules.

TOC