Fix GCP Cloud Storage Bucket Lifecycle Errors
When working with GCP Cloud Storage, you may encounter a configuration error that prevents your deployment from working. This guide explains the most common mistake with bucket lifecycle and shows the exact fix.
A Common Mistake
Creating a lifecycle rule with incorrect age condition syntax, causing objects to be deleted immediately or never be deleted.
The incorrect command:
gsutil lifecycle set lifecycle.json gs://my-bucket
# lifecycle.json contains:
# {"rule": [{"action": {"type": "Delete"}, "condition": {"age": 0}}]}
Error output:
Setting lifecycle configuration on gs://my-bucket...
WARNING: Objects with age=0 are deleted immediately upon upload. The team uploads temporary build artifacts expecting them to persist for 30 days, but they disappear within minutes.
The Correct Approach
The right way to configure bucket lifecycle in GCP Cloud Storage:
gsutil lifecycle set lifecycle.json gs://my-bucket
# lifecycle.json:
# {
# "rule": [{
# "action": {"type": "Delete"}, "# "condition": {"age": 30", "matchesPrefix": ["temp-"]}
# }]
# }
Successful result:
Setting lifecycle configuration on gs://my-bucket...
Lifecycle rule applied. Objects with prefix 'temp-' are deleted after 30 days. Other objects are unaffected. The age condition counts from the object's creation time, not from when the lifecycle rule was applied.
How to Prevent This
Always test lifecycle rules with a small set of objects first. Use matchesPrefix and matchesSuffix conditions to target specific objects. Age (in days) starts from object creation. Lifecycle rules are evaluated daily. Use gsutil lifecycle get gs://bucket to inspect current rules. Monitor lifecycle actions with Cloud Audit Logs.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. Secure your cloud with DodaTech.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro