Lifecycle Policies
What is a Lifecycle Policy?
A lifecycle policy is a set of rules automating the management of objects within a specific bucket. It can help with storage efficiency or to ensure compliance. For example, a policy may be created to automatically delete objects based on criteria such as object name, age, size, creation date, etc.
Only one lifecycle policy can be configured on a bucket, but one lifecycle policy can contain multiple rules. A Lifecycle policy is independent of a bucket policy.
Lifecycle rules are executed asynchronously and typically run once per day. The exact timing is not user-configurable. It may take up to 24 hours for a rule to be applied to an object, depending on factors like object creation time and the rule's conditions.
Key Components of a Lifecycle Policy
ID
: A unique identifier for the lifecycle rule, used for tracking and management.Filter
: Specifies which objects the rule applies to, based on prefixes or tags.- Example: Use a
Prefix
to target objects in a specific 'folder'.
- Example: Use a
Status
: Indicates whether the rule isEnabled
orDisabled
.Expiration
: Defines when objects are automatically deleted. This can be specified in:Days
: Number of days from object creation.Date
: A specific date when the rule applies.
Example Lifecycle Policies
This policy automatically:
- deletes all objects that are older than 60 days.
- deletes objects under the
logs/
prefix that are older than 30 days. - deletes all objects in the bucket on 2024-12-31.
{
"Rules": [
{
"ID": "DeleteOldObjects",
"Filter": {
"Prefix": ""
},
"Status": "Enabled",
"Expiration": {
"Days": 60
}
},
{
"ID": "DeleteOldLogs",
"Filter": {
"Prefix": "logs/"
},
"Status": "Enabled",
"Expiration": {
"Days": 30
}
},
{
"ID": "DeleteByDate",
"Filter": {
"Prefix": ""
},
"Status": "Enabled",
"Expiration": {
"Date": "2024-12-31T00:00:00.000Z"
}
}
]
}
This policy ensures that non-current versions of objects are automatically deleted after they have been non-current for 90 days. The rule has no effect if versioning is disabled on the bucket.
{
"Rules": [
{
"ID": "ExpireOldVersions",
"Status": "Enabled",
"Filter": {
"Prefix": ""
},
"NoncurrentVersionExpiration": {
"NoncurrentDays": 90
}
}
]
}
Manage Lifecycle Policies
To apply a lifecycle policy use the following command (no output if successful):
aws s3api put-bucket-lifecycle-configuration --bucket <bucket-name> --lifecycle-configuration file://lifecycle-policy.json
To check the lifecycle policy currently applied to an S3 bucket, use the following command:
If no lifecycle policy applied to the bucket you should get:
However, in some cases you might get:
This is a known issue and does not impact the fact that no lifecycle policy exists on the bucket.
To remove a bucket policy, use the following command (no output if successful):
Best Practices for Lifecycle Policies
-
Scope Policies Using Filters: Apply rules to specific 'folders' using prefixes.
-
Manage Non-Current Versions: When versioning is enabled, set rules to expire non-current object versions after a defined period to reduce storage costs.
-
Test Policies Before Deployment: Apply lifecycle rules to a non-critical test bucket first to validate their behavior. Confirm that objects are managed as expected before using the rules in production.
-
Use Descriptive Rule IDs: Assign meaningful
ID
values to lifecycle rules for easy identification and management, especially in buckets with multiple rules. -
Avoid Unintended Deletions: Double-check lifecycle rules to ensure critical data is not inadvertently deleted. Limit rules to specific prefixes or tagged objects to reduce risks.
-
Keep Policies Updated: Periodically review lifecycle policies to ensure they reflect current storage and business needs. Remove outdated or unused rules to maintain clarity.