Sign In
Sign In

Managing Object Lifecycle

Updated on 22 November 2024

When using S3, you may need to manage the lifecycle of objects to optimize resource usage and prevent storage overflow. To achieve this, you can configure lifecycle rules, which automatically delete objects after a specified number of days.

Here’s how to set up lifecycle rules for objects in an S3 bucket using the AWS CLI.

Creating a Lifecycle Rule File

To set up automatic deletion of files after a certain period, create a configuration file for the lifecycle rules. For example, if you want to store files in the logs folder for only one day, create a file named lifecycle.json with the following content:

{
  "Rules": [
    {
      "Status": "Enabled",
      "Filter": {"Prefix": "logs/"},
      "Expiration": {"Days": 1}
    }
  ]
}
  • Prefix: Specifies the folder (or prefix) to which the rule will apply. In this example, files in the logs/ folder will be automatically deleted after one day.

  • Expiration: Defines the retention period in days. Here, files are deleted one day after upload.

Adding Multiple Rules

You can add multiple rules for different folders or files. For instance, if you have another folder, logs2, where files should be stored for two days, simply add an additional rule:

{
  "Rules": [
    {
      "Status": "Enabled",
      "Filter": {"Prefix": "logs/"},
      "Expiration": {"Days": 1}
    },
    {
      "Status": "Enabled",
      "Filter": {"Prefix": "logs2/"},
      "Expiration": {"Days": 2}
    }
  ]
}

With this configuration:

  • Files in the logs/ folder will be retained for one day.
  • Files in the logs2/ folder will be retained for two days.

Uploading Lifecycle Rules to a Bucket

After creating the lifecycle rule file, upload it to your S3 bucket using the following command:

aws s3api put-bucket-lifecycle-configuration --bucket <bucket_name> --lifecycle-configuration file://lifecycle.json --endpoint-url https://s3.hostman.com

This command applies the specified rules to your bucket, and files will be automatically deleted after the defined period.

Verifying Lifecycle Rules

To ensure that the lifecycle rules were successfully applied, run the following command:

aws s3api get-bucket-lifecycle-configuration --bucket <bucket_name> --endpoint-url https://s3.hostman.com

This will display the current lifecycle rules configured for your bucket.

Removing Lifecycle Rules

If you need to delete all existing lifecycle rules for a bucket, execute the following command:

aws s3api delete-bucket-lifecycle --bucket <bucket_name> --endpoint-url https://s3.hostman.com

This command removes all lifecycle rules, and files will no longer be automatically deleted.

Was this page helpful?
Updated on 22 November 2024

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support