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 Hostman control panel and the AWS CLI.

Via the Control Panel

Creating Rules

To create lifecycle rules:

  1. Go to the S3 Storage section and click on the bucket.
  2. In the Settings tab, click Change next to Lifecycle.

Cdf1f9d8 689b 4713 Aec4 5fb1730689af

  1. Click Add rule.
  2. Set up the parameters:
    • Object prefix. For example, if you set the prefix logs/, the rule will only apply to objects in the logs “folder.”
    • Action. Choose what should happen to the objects and after how many days the action should be applied. Three options are available:
      • Delete any: applies to any object in the bucket.
      • Delete non-current: applies to old versions of objects (relevant if object versioning is enabled).
      • Abort incomplete upload: automatically removes unfinished multipart uploads.
  1. Click Save.

23ad3920 2560 4904 9d02 Ecb1da936265

Managing Existing Rules

To view and manage all existing lifecycle rules:

  1. Go to the S3 Storage section and click on the bucket.
  2. In the Settings tab, click Change next to Lifecycle.

The window will display all existing rules, including those added through external tools such as the AWS CLI.

Here, you can:

  • Delete a rule by clicking the trash bin icon.
  • Temporarily disable a rule by toggling the switch.
  • Edit a rule by clicking on it to open the edit window.

Fee1d050 Ea32 4ab6 B6e9 66f9996f2cb0

Via 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