Overview and What You Will Learn
In this lab, you will build a Lifecycle policy with multiple rules - one transitioning objects to a colder class by age, another deleting objects entirely after a longer retention period - and verify the policy's conditions actually match the objects intended.
Why This Matters in Production
A team manually reclassifies old log files to a colder storage tier once a quarter, whenever someone remembers to do it - meaning for weeks at a time, aging data sits in a more expensive tier than it needs to, and the "manual quarterly cleanup" task is exactly the kind of recurring chore that eventually stops happening reliably once the person who used to do it moves to a different team.
Core Principles
A Lifecycle rule pairs a condition (like object age, or a specific storage class) with an action (like transitioning to a colder class, or deleting the object outright) - once configured, Cloud Storage evaluates every object against these rules automatically and continuously, with no manual intervention required.
+------------------------------------------+| Lifecycle Rule || || Condition: age >= 90 days || Action: SetStorageClass -> COLDLINE |+------------------------------------------+| Lifecycle Rule || || Condition: age >= 2555 days (7 years) || Action: Delete |+------------------------------------------+A bucket can have multiple Lifecycle rules simultaneously, each evaluated independently against every object - this lets a single bucket age data through several storage classes over time before eventually being deleted, all through one configured policy.
Detailed Step-by-Step Practical Lab
- Create a project and a bucket:
gcloud projects create gcp-lifecycle-lab-2026 --name="Lifecycle Lab"gcloud config set project gcp-lifecycle-lab-2026 gcloud storage buckets create gs://lifecycle-lab-bucket-2026 \ --location=asia-south1- Upload a few test objects:
echo "log entry 1" > log1.txtecho "log entry 2" > log2.txtgcloud storage cp log1.txt log2.txt gs://lifecycle-lab-bucket-2026/- Build a multi-rule Lifecycle policy - transition to Nearline after 30 days, Coldline after 90 days, and delete entirely after 7 years:
cat > lifecycle-config.yaml << 'LIFECYCLEEOF'rule: - action: type: SetStorageClass storageClass: NEARLINE condition: age: 30 - action: type: SetStorageClass storageClass: COLDLINE condition: age: 90 - action: type: Delete condition: age: 2555LIFECYCLEEOF- Apply the policy to the bucket:
gcloud storage buckets update gs://lifecycle-lab-bucket-2026 \ --lifecycle-file=lifecycle-config.yaml- Confirm the policy was applied correctly:
gcloud storage buckets describe gs://lifecycle-lab-bucket-2026 \ --format="value(lifecycle)"- Add a condition targeting only objects under a specific prefix, so different folders within the same bucket can age differently:
cat > lifecycle-config-scoped.yaml << 'LIFECYCLEEOF'rule: - action: type: SetStorageClass storageClass: ARCHIVE condition: age: 365 matchesPrefix: - "compliance-archives/"LIFECYCLEEOF gcloud storage buckets update gs://lifecycle-lab-bucket-2026 \ --lifecycle-file=lifecycle-config-scoped.yamlNote
matchesPrefixscopes a rule to only objects whose key starts with the given prefix, similar in concept to how an AWS S3 Lifecycle rule can be scoped to a specific folder path. Without this, a single bucket-wide rule applies identically to every object regardless of which logical "folder" it sits in.
- Clean up:
gcloud storage rm --recursive gs://lifecycle-lab-bucket-2026gcloud projects delete gcp-lifecycle-lab-2026 --quietProduction Best Practices & Common Pitfalls
Common MistakeWriting a Lifecycle rule with an age condition that conflicts with actual business retention requirements - like a rule deleting data after 90 days when a compliance requirement mandates keeping it for 7 years. Always confirm the actual required retention period before writing a delete rule, since a wrongly-configured delete action is not reversible once it executes.
TipTest a new Lifecycle rule's conditions against a small, non-critical bucket first, and review which objects it would actually match before applying it broadly. A subtly wrong age or prefix condition can silently affect far more (or fewer) objects than intended.
- Lifecycle actions apply to existing objects immediately upon policy creation, not just to objects uploaded afterward - an object already 100 days old when a 90-day transition rule is added will be transitioned on the next evaluation cycle, not exempted just because it predates the rule.
- Combine Lifecycle rules with Object Versioning carefully. If versioning is enabled, Lifecycle conditions can also target non-current (older) versions specifically, letting you keep only the current version at Standard cost while aging out old versions faster.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud storage buckets update --lifecycle-file= |
Apply a Lifecycle policy from a YAML or JSON file |
gcloud storage buckets describe --format="value(lifecycle)" |
View a bucket's current Lifecycle policy |
gcloud storage objects describe |
Check a specific object's current storage class and age |
gcloud storage buckets update --clear-lifecycle |
Remove all Lifecycle rules from a bucket |