Overview and What You Will Learn
A Cloud Storage Lifecycle rule pairs a condition, such as object age or prefix, with an action, either moving the object to a colder storage class or deleting it. Once the rules are set on a bucket, Cloud Storage checks every object against them automatically, so there is no scheduled job to build or maintain.
In this lab, you will:
- Build one policy that moves objects between storage classes by age and deletes them after a retention period
- Scope rules to a prefix so different folders age at different speeds
- Clean up old object versions with versioning-aware conditions
- Verify the applied policy and avoid overwriting it by accident
Why This Matters in Production
Lifecycle rules replace a recurring manual chore with a policy that lives on the bucket itself. Picture a data platform team at a company like Spotify that reclassifies old logs to a colder tier by hand once a quarter, whenever someone remembers. For weeks at a time aging data sits in an expensive class, and the chore stops entirely once its owner changes teams. A Lifecycle policy keeps working no matter who owns the task.
Core Principles
Each rule is evaluated independently against every object, so one bucket can age data through several classes and finally delete it. A rule has exactly two parts:
- Action is what happens:
SetStorageClassmoves the object to a colder class, andDeleteremoves it. - Condition is when it happens: object age, a name prefix or suffix, the current storage class, or whether the object is a live or noncurrent version.
RememberLifecycle only moves objects to a colder class, never back to a warmer one. Age counts from the object's creation time, and evaluation is asynchronous, so an action can lag by up to a day after the condition is met.
Detailed Step-by-Step Practical Lab
How to Set Up a Sandbox Project and Bucket
Use a throwaway project with billing linked. Bucket names are globally unique, so replace the rahul part with your own suffix.
export PROJECT_ID="mumbai-storage-lab-2026"export BUCKET="gs://lifecycle-lab-rahul-2026" gcloud projects create $PROJECT_ID --name="Storage Lab"gcloud config set project $PROJECT_ID gcloud billing accounts listgcloud billing projects link $PROJECT_ID --billing-account=0X0X0X-0X0X0X-0X0X0Xgcloud services enable storage.googleapis.com # Create the bucket with object versioning turned ongcloud storage buckets create $BUCKET --location=asia-south1gcloud storage buckets update $BUCKET --versioningHow to Upload Objects Under Two Prefixes
echo "app log entry" > app.logecho "compliance record" > record.txtgcloud storage cp app.log $BUCKET/app-logs/app.loggcloud storage cp record.txt $BUCKET/compliance-archives/record.txtHow to Write a Lifecycle Policy File
Lifecycle policies are JSON files, and one file holds every rule for the bucket. This file covers four jobs: logs move to Nearline then Coldline, compliance records move to Archive and are deleted after 7 years, and old noncurrent versions are removed 30 days after they are replaced.
cat > lifecycle-config.json << 'LIFECYCLEEOF'{ "rule": [ { "action": { "type": "SetStorageClass", "storageClass": "NEARLINE" }, "condition": { "age": 30, "matchesStorageClass": ["STANDARD"], "matchesPrefix": ["app-logs/"] } }, { "action": { "type": "SetStorageClass", "storageClass": "COLDLINE" }, "condition": { "age": 90, "matchesStorageClass": ["NEARLINE"], "matchesPrefix": ["app-logs/"] } }, { "action": { "type": "SetStorageClass", "storageClass": "ARCHIVE" }, "condition": { "age": 365, "matchesPrefix": ["compliance-archives/"] } }, { "action": { "type": "Delete" }, "condition": { "age": 2555, "matchesPrefix": ["compliance-archives/"] } }, { "action": { "type": "Delete" }, "condition": { "isLive": false, "daysSinceNoncurrentTime": 30 } } ]}LIFECYCLEEOFHow to Apply a Lifecycle Policy to a Bucket
gcloud storage buckets update $BUCKET --lifecycle-file=lifecycle-config.jsonCommon MistakeApplying a second lifecycle file expecting it to add a rule. Each update replaces the bucket's entire policy, so always edit the full file and re-apply it.
How to Verify the Applied Lifecycle Rules
Confirm that all five rules are present exactly as written.
gcloud storage buckets describe $BUCKET --format="json(lifecycle_config)"How to Check Which Objects a Rule Will Affect
List objects with their creation times, then compare them against each rule's age and prefix.
gcloud storage ls --long --recursive $BUCKET/Fresh uploads match no age rule yet, so nothing will move during the lab. The value of this step is validating the configuration, not watching a transition happen.
TipTo see a real transition without waiting 30 days, set a past Custom-Time on a test object with
gcloud storage objects update --custom-time=2025-01-01T00:00:00Zand add a rule using thedaysSinceCustomTimecondition. Allow up to a day for evaluation.
How to Clean Up
Remove the policy, delete every object version and the bucket, then delete the project.
gcloud storage buckets update $BUCKET --clear-lifecyclegcloud storage rm --recursive --all-versions $BUCKETgcloud projects delete $PROJECT_ID --quietProduction Best Practices & Common Pitfalls
Common MistakeWriting a delete rule whose age conflicts with a business or compliance retention requirement, such as deleting after 90 days when the data must be kept for 7 years. Confirm the required retention first, because a delete action cannot be undone.
TipTest new conditions on a small, non-critical bucket and review which objects match before applying broadly. A subtly wrong age or prefix can affect far more or far fewer objects than intended.
SecurityA Lifecycle rule is not a lock. Anyone with permission to update the bucket can change or remove it, so use a retention policy or Bucket Lock when data must not be deleted early.
- Rules apply to existing objects too. An object already 100 days old when you add a 90-day rule is transitioned on the next evaluation, not exempted because it predates the rule.
- Delete wins conflicts. If a Delete rule and a SetStorageClass rule both match an object, the delete is applied.
- Deleting a live object in a versioned bucket keeps a copy. The object becomes noncurrent, so pair every Delete rule with a noncurrent-version rule or you will keep paying for hidden versions.
- Transitions are billable operations. Each class change is a Class A operation, so moving millions of tiny objects can cost more than it saves.
- Mind minimum durations. Deleting an object before its class minimum passes still bills the full minimum.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud storage buckets update --lifecycle-file= |
Replace the policy from a JSON file |
gcloud storage buckets describe --format="json(lifecycle_config)" |
View the current policy |
gcloud storage ls --long --recursive |
Inspect object creation times and sizes |
gcloud storage buckets update --versioning |
Enable object versioning |
gcloud storage buckets update --clear-lifecycle |
Remove all Lifecycle rules |
gcloud storage rm --recursive --all-versions |
Delete objects, all versions, and the bucket |