0
0
GCPcloud~5 mins

Lifecycle management rules in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Lifecycle management rules help you automatically manage files in cloud storage. They let you set simple rules to delete or move files after a certain time, saving space and costs without manual work.
When you want to automatically delete old backup files after 30 days to save storage costs.
When you want to move files to cheaper storage after they haven't been accessed for 60 days.
When you want to keep only the latest versions of files and delete older ones automatically.
When you want to archive logs that are older than 90 days to a cold storage class.
When you want to clean up temporary files that are no longer needed after a few days.
Config File - lifecycle.json
lifecycle.json
{
  "rule": [
    {
      "action": {"type": "Delete"},
      "condition": {"age": 30}
    },
    {
      "action": {"type": "SetStorageClass", "storageClass": "NEARLINE"},
      "condition": {"age": 60}
    }
  ]
}

This JSON file defines lifecycle rules for a Google Cloud Storage bucket.

rule: A list of rules to apply.

action: What to do with the files. Here, delete files older than 30 days, and move files older than 60 days to NEARLINE storage.

condition: When to apply the action, based on file age in days.

Commands
This command applies the lifecycle rules defined in lifecycle.json to the bucket named example-bucket. It tells Google Cloud Storage to start managing files automatically based on these rules.
Terminal
gsutil lifecycle set lifecycle.json gs://example-bucket
Expected OutputExpected
Setting lifecycle configuration on gs://example-bucket/...
This command retrieves and shows the current lifecycle rules set on the example-bucket. It helps verify that the rules were applied correctly.
Terminal
gsutil lifecycle get gs://example-bucket
Expected OutputExpected
{ "rule": [ { "action": {"type": "Delete"}, "condition": {"age": 30} }, { "action": {"type": "SetStorageClass", "storageClass": "NEARLINE"}, "condition": {"age": 60} } ] }
Key Concept

If you remember nothing else from this pattern, remember: lifecycle rules automate file cleanup and storage class changes based on file age to save costs and effort.

Common Mistakes
Not applying the lifecycle rules to the correct bucket name.
The rules won't affect the intended storage bucket, so files won't be managed automatically.
Double-check the bucket name in the gsutil lifecycle set command matches your target bucket.
Using incorrect JSON syntax in the lifecycle configuration file.
The gsutil command will fail to apply the rules because the file is invalid.
Validate the JSON file syntax before applying, ensuring proper brackets, commas, and quotes.
Expecting immediate deletion or storage class change after applying rules.
Lifecycle actions run asynchronously and may take some time to process files.
Wait some time after applying rules and check file status later to see changes.
Summary
Create a JSON file defining lifecycle rules with actions and conditions.
Use gsutil lifecycle set to apply these rules to your storage bucket.
Verify the rules with gsutil lifecycle get to ensure they are set correctly.