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.
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.
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:
logs/
folder will be retained for one day.logs2/
folder will be retained for two days.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.
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.
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.