0
0
GcpHow-ToBeginner · 3 min read

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 like age, createdBefore, or isLive.
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-name
Output
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 action and condition.
  • 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 gsutil command.
json
{
  "rule": [
    {
      "action": {"type": "Delete"}
      /* Missing condition causes error */
    }
  ]
}

/* Corrected version: */
{
  "rule": [
    {
      "action": {"type": "Delete"},
      "condition": {"age": 30}
    }
  ]
}
📊

Quick Reference

FieldDescriptionExample
action.typeType of action to performDelete, SetStorageClass
action.storageClassTarget storage class for SetStorageClass actionNEARLINE, COLDLINE, ARCHIVE
condition.ageObject age in days to trigger action30
condition.createdBeforeDate before which objects are affected (YYYY-MM-DD)2023-01-01
condition.isLiveWhether the object is live (true or false)true
condition.numNewerVersionsNumber of newer versions for versioned buckets3

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.