This page describes how to enable Object Lifecycle Management and gives examples of lifecycle configurations. Lifecycle configurations are managed at the bucket level and apply to all current and future objects in the bucket.
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 on the bucket's name.
Click on the Lifecycle tab.
The lifecycle rules page appears.
Click Add a rule.
In the page that appears, specify a configuration.
Select the action to take when an object meets the conditions.
Click Continue.
Select the conditions under which an action is taken.
Click Continue.
Click Create.
See Troubleshooting for how to get detailed error information about failed operations in the Cloud Storage browser.
gsutil
Create a
.json
file with the lifecycle configuration rules you would like to apply. See configuration examples for sample.json
files.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
.
Code samples
For more information, see the
Cloud Storage C++ API reference documentation.
For more information, see the
Cloud Storage C# API reference documentation.
For more information, see the
Cloud Storage Go API reference documentation.
For more information, see the
Cloud Storage Java API reference documentation.
For more information, see the
Cloud Storage Node.js API reference documentation.
For more information, see the
Cloud Storage PHP API reference documentation.
For more information, see the
Cloud Storage Python API reference documentation.
For more information, see the
Cloud Storage Ruby API reference documentation.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
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 configuration examples for sample.json
files.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 configuration examples for sample.xml
files.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 on the entry in the Lifecycle rules column.
The lifecycle rules page appears.
Click Delete all.
In the confirmation window that appears, click Confirm.
See Troubleshooting for how to get detailed error information about failed operations in the Cloud Storage browser.
gsutil
Create a
.json
file with an empty lifecycle configuration:{ "lifecycle": { "rule": [] } }
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
.
Code samples
For more information, see the
Cloud Storage C++ API reference documentation.
For more information, see the
Cloud Storage C# API reference documentation.
For more information, see the
Cloud Storage Go API reference documentation.
For more information, see the
Cloud Storage Java API reference documentation.
For more information, see the
Cloud Storage Node.js API reference documentation.
For more information, see the
Cloud Storage PHP API reference documentation.
For more information, see the
Cloud Storage Python API reference documentation.
For more information, see the
Cloud Storage Ruby API reference documentation.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
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:{ "lifecycle": { "rule": [] } }
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.
See Troubleshooting for how to get detailed error information about failed operations in the Cloud Storage browser.
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 perform lifecycle actions when common lifecycle conditions are met. These configurations apply to all current and future objects in the configured bucket.
Delete an object
The following lifecycle configuration defines three rules. Note that the second and third rules are applicable only when using Object Versioning on the bucket:
Delete live versions of objects older than 30 days.
If the bucket uses Object Versioning, such objects become noncurrent and are subject to the other two rules.
If the bucket does not use Object Versioning, such objects are permanently deleted and cannot be recovered.
Delete noncurrent versions of objects if there are 2 newer versions of the object in the bucket. Objects subject to this rule are permanently deleted and cannot be recovered.
Delete noncurrent versions of objects older than 35 days. Objects subject to this rule are permanently deleted and cannot be recovered.
The combined effect of these three rules is that objects are subject to permanent deletion after a maximum of 35 days in the bucket: 30 days as a live version and 5 days as a noncurrent version. For live object versions replaced or deleted outside the scope of this configuration, they become subject to permanent deletion sooner: either 5 days from the time they became noncurrent or whenever there is more than one newer version of the object in the bucket.
Console
These settings use the instructions found in the enable section.
To delete live versions of objects older than 30 days:
- Select the Delete object action.
- Select the Age condition, and enter a value of 30.
- Select the Live state condition, and choose the Live option.
To delete noncurrent versions of objects if there are 2 newer versions:
- Select the Delete object action.
- Select the Number of newer versions condition, and enter a value of 2.
To delete noncurrent versions of objects older than 35 days:
- Select the Delete object action.
- Select the Age condition, and enter a value of 35.
- Select the Live state condition, and choose the Noncurrent option.
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": { "numNewerVersions": 2 } }, { "action": {"type": "Delete"}, "condition": { "age": 35, "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": { "numNewerVersions": 2 } }, { "action": {"type": "Delete"}, "condition": { "age": 35, "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> <NumberOfNewerVersions>2</Age> </Condition> </Rule> <Rule> <Action> <Delete/> </Action> <Condition> <IsLive>false</IsLive> <Age>35</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
These settings use the instructions found in the enable section.
To moves objects to Cloud Storage Nearline 365 days after they were created:
- Select the Set storage class to Nearline action.
- Select the Age condition, and enter a value of 365.
- Select the Storage class matches condition, and choose the Standard, Multi-Regional, and Durable Reduced Availability options.
To moves objects to Cloud Storage Coldline 1095 days after they were created:
- Select the Set storage class to Coldline action.
- Select the Age condition, and enter a value of 1095.
- Select the Storage class matches condition, and choose the Nearline option.
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.
What's next
- Learn more about Object Lifecycle Management, including the available lifecycle actions and lifecycle conditions.