How to Set Lifecycle Rule in Google Cloud Storage
To set a lifecycle rule in Google Cloud Storage, define a
lifecycle configuration in your bucket that specifies conditions and actions like deleting or changing storage class. You can apply this using the gsutil lifecycle set command with a JSON file or via the Google Cloud Console.Syntax
A lifecycle rule in Google Cloud Storage consists of conditions and actions. Conditions specify when the rule applies, such as object age or storage class. Actions define what happens, like deleting the object or changing its storage class.
The lifecycle configuration is a JSON object with a rule array. Each rule has:
action: The operation to perform (e.g.,Delete,SetStorageClass).condition: Criteria likeage,createdBefore, orisLive.
json
{
"rule": [
{
"action": {"type": "Delete"},
"condition": {"age": 30}
},
{
"action": {"type": "SetStorageClass", "storageClass": "COLDLINE"},
"condition": {"age": 60}
}
]
}Example
This example shows how to create a lifecycle rule that deletes objects older than 365 days and moves objects older than 180 days to the NEARLINE storage class.
You apply this rule to a bucket using the gsutil lifecycle set command.
json + shell
{
"rule": [
{
"action": {"type": "Delete"},
"condition": {"age": 365}
},
{
"action": {"type": "SetStorageClass", "storageClass": "NEARLINE"},
"condition": {"age": 180}
}
]
}
# Command to apply the lifecycle rule:
gsutil lifecycle set lifecycle.json gs://your-bucket-nameOutput
Setting lifecycle configuration on gs://your-bucket-name/...
Common Pitfalls
- Incorrect JSON format: The lifecycle JSON must be valid and follow the schema exactly.
- Missing required fields: Each rule must have both
actionandcondition. - Conflicting rules: Overlapping conditions can cause unexpected behavior; order does not matter but clarity helps.
- Applying to wrong bucket: Ensure you specify the correct bucket name in the
gsutilcommand.
json
{
"rule": [
{
"action": {"type": "Delete"}
/* Missing condition causes error */
}
]
}
/* Corrected version: */
{
"rule": [
{
"action": {"type": "Delete"},
"condition": {"age": 30}
}
]
}Quick Reference
| Field | Description | Example |
|---|---|---|
| action.type | Type of action to perform | Delete, SetStorageClass |
| action.storageClass | Target storage class for SetStorageClass action | NEARLINE, COLDLINE, ARCHIVE |
| condition.age | Object age in days to trigger action | 30 |
| condition.createdBefore | Date before which objects are affected (YYYY-MM-DD) | 2023-01-01 |
| condition.isLive | Whether the object is live (true or false) | true |
| condition.numNewerVersions | Number of newer versions for versioned buckets | 3 |
Key Takeaways
Define lifecycle rules as JSON with clear action and condition pairs.
Use gsutil lifecycle set command to apply rules to your bucket.
Always validate your JSON to avoid errors when setting lifecycle rules.
Lifecycle rules automate object management, saving cost and effort.
Test rules on non-critical buckets before applying to production.