This page describes how to enable Object Lifecycle Management and gives examples of lifecycle policies. For a description of this feature, including the actions and conditions it supports, see Object Lifecycle Management.
Setting up lifecycle management on a bucket
Enabling Lifecycle Management
To enable lifecycle management for a bucket:
Console
- Open the Cloud Storage browser in the Google Cloud Console.
Open the Cloud Storage browser In the bucket list, find the bucket you want to enable, and click None in the Lifecycle column.
The lifecycle rules page appears.
Click Add rule.
In the page that appears, specify a configuration.
Select the condition(s) under which an action is taken.
Click Continue.
Select the action to take when an object meets the condition(s).
Click Continue.
Click Save.
gsutil
Create a
.json
file with the lifecycle configuration rules you would like to apply (see examples).Use the
lifecycle set
command to apply the configuration:gsutil lifecycle set [LIFECYCLE_CONFIG_FILE] gs://[BUCKET_NAME]
Where:
[LIFECYCLE_CONFIG_FILE]
is the name of the file you created in Step 1.[BUCKET_NAME]
is the name of the relevant bucket. For example,my-bucket
.
REST APIs
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
Create a
.json
file with the lifecycle configuration rules you would like to apply (see examples).Use
cURL
to call the JSON API with aPATCH
Bucket request:curl -X PATCH --data-binary @[LIFECYCLE_CONFIG_FILE].json \ -H "Authorization: Bearer [OAUTH2_TOKEN]" \ -H "Content-Type: application/json" \ "https://storage.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=lifecycle"
Where:
[LIFECYCLE_CONFIG_FILE]
is the name of the file you created in Step 2.[OAUTH2_TOKEN]
is the access token you generated in Step 1.[BUCKET_NAME]
is the name of the relevant bucket. For example,my-bucket
.
XML API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
Create a
.xml
file with the lifecycle configuration rules you would like to apply (see examples).Use
cURL
to call the XML API with aPUT
Bucket request:curl -X PUT --data-binary @[XML_FILE].xml \ -H "Authorization: Bearer [OAUTH2_TOKEN]" \ "https://storage.googleapis.com/[BUCKET_NAME]?lifecycle"
Where:
[XML_FILE]
is the name of the file you created in Step 2.[OAUTH2_TOKEN]
is the access token you generated in Step 1.[BUCKET_NAME]
is the name of the relevant bucket. For example,my-bucket
.
Disabling Lifecycle Management
To disable lifecycle management for a bucket:
Console
- Open the Cloud Storage browser in the Google Cloud Console.
Open the Cloud Storage browser In the bucket list, find the bucket you want to disable, and click Enabled in the Lifecycle column.
The lifecycle rules page appears.
Click Delete all.
In the confirmation window that appears, click Delete.
gsutil
Create a
.json
file with an empty lifecycle configuration:{}
Use the
lifecycle set
command:gsutil lifecycle set [LIFECYCLE_CONFIG_FILE] gs://[BUCKET_NAME]
Where:
[LIFECYCLE_CONFIG_FILE]
is the name of the file you created in Step 1.[BUCKET_NAME]
is the name of the relevant bucket. For example,my-bucket
.
REST APIs
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
Create a
.json
file with an empty lifecycle configuration:{}
Use
cURL
to call the JSON API with aPATCH
Bucket request:curl -X PATCH --data-binary @[LIFECYCLE_CONFIG_FILE].json \ -H "Authorization: Bearer [OAUTH2_TOKEN]" \ -H "Content-Type: application/json" \ "https://storage.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=lifecycle"
Where:
[LIFECYCLE_CONFIG_FILE]
is the name of the file you created in Step 2.[OAUTH2_TOKEN]
is the access token you generated in Step 1.[BUCKET_NAME]
is the name of the relevant bucket. For example,my-bucket
.
XML API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
Create a
.xml
file with an empty lifecycle configuration:<LifecycleConfiguration/>
Use
cURL
to call the XML API with aPUT
Bucket request:curl -X PUT --data-binary @[XML_FILE].xml \ -H "Authorization: Bearer [OAUTH2_TOKEN]" \ "https://storage.googleapis.com/[BUCKET_NAME]?lifecycle"
Where:
[XML_FILE]
is the name of the file you created in Step 2.[OAUTH2_TOKEN]
is the access token you generated in Step 1.[BUCKET_NAME]
is the name of the relevant bucket. For example,my-bucket
.
Checking Lifecycle Configuration
To check the lifecycle configuration set on a bucket:
Console
- Open the Cloud Storage browser in the Google Cloud Console.
Open the Cloud Storage browser In the bucket list, the lifecycle status of each bucket is found in the Lifecycle column.
You can click on this status to add, view, edit, and delete rules.
gsutil
Use the
lifecycle get
command:gsutil lifecycle get gs://[BUCKET_NAME]
Where
[BUCKET_NAME]
is the name of the relevant bucket. For example,my-bucket
.
REST APIs
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
Use
cURL
to call the JSON API with aGET
Bucket request:curl -X GET \ -H "Authorization: Bearer [OAUTH2_TOKEN]" \ "https://storage.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=lifecycle"
Where:
[OAUTH2_TOKEN]
is the access token you generated in Step 1.[BUCKET_NAME]
is the name of the relevant bucket. For example,my-bucket
.
XML API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
Use
cURL
to call the XML API with aGET
Bucket request:curl -X GET \ -H "Authorization: Bearer [OAUTH2_TOKEN]" \ "https://storage.googleapis.com/[BUCKET_NAME]?lifecycle"
Where:
[OAUTH2_TOKEN]
is the access token you generated in Step 1.[BUCKET_NAME]
is the name of the relevant bucket. For example,my-bucket
.
Object lifecycle configuration examples
The following examples show specific lifecycle configurations that accomplish deleting objects and changing the storage class of objects when common criteria are met. For a detailed discussion of lifecycle configurations, including a list of available actions and conditions, see Object Lifecycle Management.
Delete an object
The following lifecycle configuration defines two rules:
- Delete live versions of objects older than 30 days. Note that all of your objects are considered live unless you use object versioning.
- Delete noncurrent versions of objects older than 10 days. Note that this rule is only ever met for buckets where you have - or previously had - used object versioning.
Console
The following rule deletes live versions of objects older than 30 days:
The following rule deletes noncurrent versions of objects older than 10 days:
gsutil
The following lifecycle configuration can be applied to a bucket by enabling lifecycle management.
{ "lifecycle": { "rule": [ { "action": {"type": "Delete"}, "condition": { "age": 30, "isLive": true } }, { "action": {"type": "Delete"}, "condition": { "age": 10, "isLive": false } } ] } }
For the generalized format of a lifecycle configuration file, see the bucket resource representation for JSON.
REST APIs
JSON API
The following lifecycle configuration can be applied to a bucket by enabling lifecycle management.
{ "lifecycle": { "rule": [ { "action": {"type": "Delete"}, "condition": { "age": 30, "isLive": true } }, { "action": {"type": "Delete"}, "condition": { "age": 10, "isLive": false } } ] } }
For the generalized format of a lifecycle configuration file, see the bucket resource representation for JSON.
XML API
The following lifecycle configuration can be applied to a bucket by enabling lifecycle management.
<?xml version="1.0" encoding="UTF-8" ?> <LifecycleConfiguration> <Rule> <Action> <Delete/> </Action> <Condition> <IsLive>true</IsLive> <Age>30</Age> </Condition> </Rule> <Rule> <Action> <Delete/> </Action> <Condition> <IsLive>false</IsLive> <Age>10</Age> </Condition> </Rule> </LifecycleConfiguration>
For the generalized format of a lifecycle configuration file, see the lifecycle configuration format for XML.
Change an object's storage class
The following lifecycle configuration defines two rules:
- Change the storage class of an object to Nearline Storage if both its age is greater than 365 days (one year) and its current storage class is Standard Storage, Multi-Regional Storage, or Durable Reduced Availability (DRA) Storage.
- Change the storage class of an object to Coldline Storage if both its age is greater than 1095 days (three years) and its current storage class is Nearline Storage.
Console
The following rule moves objects to Nearline 365 days after the object was created:
The following rule moves objects to Coldline 1095 days after the object was created:
gsutil
The following lifecycle configuration can be applied to a bucket by enabling lifecycle management.
{ "lifecycle": { "rule": [ { "action": { "type": "SetStorageClass", "storageClass": "NEARLINE" }, "condition": { "age": 365, "matchesStorageClass": ["MULTI_REGIONAL", "STANDARD", "DURABLE_REDUCED_AVAILABILITY"] } }, { "action": { "type": "SetStorageClass", "storageClass": "COLDLINE" }, "condition": { "age": 1095, "matchesStorageClass": ["NEARLINE"] } } ] } }
For the generalized format of a lifecycle configuration file, see the bucket resource representation for JSON.
REST APIs
JSON API
The following lifecycle configuration can be applied to a bucket by enabling lifecycle management.
{ "lifecycle": { "rule": [ { "action": { "type": "SetStorageClass", "storageClass": "NEARLINE" }, "condition": { "age": 365, "matchesStorageClass": ["MULTI_REGIONAL", "STANDARD", "DURABLE_REDUCED_AVAILABILITY"] } }, { "action": { "type": "SetStorageClass", "storageClass": "COLDLINE" }, "condition": { "age": 1095, "matchesStorageClass": ["NEARLINE"] } } ] } }
For the generalized format of a lifecycle configuration file, see the bucket resource representation for JSON.
XML API
The following lifecycle configuration can be applied to a bucket by enabling lifecycle management.
<?xml version="1.0" encoding="UTF-8" ?> <LifecycleConfiguration> <Rule> <Action> <SetStorageClass>NEARLINE</SetStorageClass> </Action> <Condition> <Age>365</Age> <MatchesStorageClass>MULTI_REGIONAL</MatchesStorageClass> <MatchesStorageClass>STANDARD</MatchesStorageClass> <MatchesStorageClass>DURABLE_REDUCED_AVAILABILITY</MatchesStorageClass> </Condition> </Rule> <Rule> <Action> <SetStorageClass>COLDLINE</SetStorageClass> </Action> <Condition> <Age>1095</Age> <MatchesStorageClass>NEARLINE</MatchesStorageClass> </Condition> </Rule> </LifecycleConfiguration>
For the generalized format of a lifecycle configuration file, see the lifecycle configuration format for XML.