This page shows example configurations for Object Lifecycle Management. When you set a lifecycle configuration on a bucket, current and future objects in the bucket that meet the conditions you define have the specified action performed on them.
Delete older object versions
The following lifecycle configuration defines two rules. Note that these rules are applicable only when using Object Versioning on the bucket:
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 after they've been noncurrent for 7 days. Objects subject to this rule are permanently deleted and cannot be recovered.
The combined effect of these two rules in buckets that use Object Versioning is that live objects have a window of time in which they can be recovered if they get overwritten or deleted: such an object remains in the bucket in a noncurrent state for seven days or until 2 newer versions of the object exist in the bucket.
Console
These settings use the instructions found in the set a configuration section.
To delete noncurrent versions of objects if there is a newer version:
- Select the Delete object action.
- Select the Live state condition, and select the Noncurrent radio button.
- Select the Number of newer versions condition, and enter a value of 2.
To delete noncurrent versions of objects after they're been noncurrent for 7 days:
- Select the Delete object action.
- Select the Days since becoming noncurrent condition, and enter a value of 7.
Command line
The following lifecycle configuration can be applied to a bucket by enabling lifecycle management.
{ "lifecycle": { "rule": [ { "action": {"type": "Delete"}, "condition": { "numNewerVersions": 2, "isLive": false } }, { "action": {"type": "Delete"}, "condition": { "daysSinceNoncurrentTime": 7 } } ] } }
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": { "numNewerVersions": 2, "isLive": false } }, { "action": {"type": "Delete"}, "condition": { "daysSinceNoncurrentTime": 7 } } ] } }
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>false</IsLive> <NumberOfNewerVersions>2</Age> </Condition> </Rule> <Rule> <Action> <Delete/> </Action> <Condition> <DaysSinceNoncurrentTime>7</DaysSinceNoncurrentTime> </Condition> </Rule> </LifecycleConfiguration>
For the generalized format of a lifecycle configuration file, see the lifecycle configuration format for XML.
Configuration modifications
Depending on your goals, there are several modifications you could make to this example configuration:
If you wanted your live objects to have a limited lifetime, create an additional rule that uses the
Delete
action, anisLive
condition set totrue
, and anage
condition.If you wanted to protect your data from accidental deletion but not accidental overwrite, set the number of newer versions to 1, which also reduces storage costs, because you retain fewer versions of an object.
- If your data is stored as Nearline storage, Coldline storage, or
Archive storage, add an
age
condition or use a larger value fordaysSinceNoncurrent
to avoid early deletion fees. Note that even with such changes, you might incur early deletion fees, depending on how frequently you overwrite and delete your objects.
Change an object's storage class
The following lifecycle configuration defines two rules:
- Change the storage class of an object to Nearline storage if its
age is greater than 365 days (one year), and its current storage class
is Standard storage, and its name ends in either
.jpg
or.png
. - Change the storage class of an object to Coldline storage if its age is
greater than 1095 days (three years), and its current storage class is
Nearline storage, and its name ends in either
.jpg
or.png
.
Console
These settings use the instructions found in the set a configuration section.
To move the objects to Nearline storage 365 days after they were created:
- Select the Set storage class to Nearline action.
- Select the Object name matches suffix rule scope.
- In the field that appears, type .jpg, and press enter. Then type .png, and press enter again.
- Select the Age condition, and enter a value of 365.
- Select the Storage class matches condition, and choose the Standard option.
To move the objects to Coldline storage 1095 days after they were created:
- Select the Set storage class to Coldline action.
- Select the Object name matches suffix rule scope.
- In the field that appears, type .jpg, and press enter. Then type .png, and press enter again.
- Select the Age condition, and enter a value of 1095.
- Select the Storage class matches condition, and choose the Nearline option.
Command line
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": [ "STANDARD" ], "matchesSuffix": [ ".jpg", ".png" ] } }, { "action": { "type": "SetStorageClass", "storageClass": "COLDLINE" }, "condition": { "age": 1095, "matchesStorageClass": [ "NEARLINE" ], "matchesSuffix": [ ".jpg", ".png" ] } } ] } }
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": [ "STANDARD" ], "matchesSuffix": [ ".jpg", ".png" ] } }, { "action": { "type": "SetStorageClass", "storageClass": "COLDLINE" }, "condition": { "age": 1095, "matchesStorageClass": [ "NEARLINE" ], "matchesSuffix": [ ".jpg", ".png" ] } } ] } }
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>STANDARD</MatchesStorageClass> <MatchesSuffix>.jpg</MatchesSuffix> <MatchesSuffix>.png</MatchesSuffix> </Condition> </Rule> <Rule> <Action> <SetStorageClass>COLDLINE</SetStorageClass> </Action> <Condition> <Age>1095</Age> <MatchesStorageClass>NEARLINE</MatchesStorageClass> <MatchesSuffix>.jpg</MatchesSuffix> <MatchesSuffix>.png</MatchesSuffix> </Condition> </Rule> </LifecycleConfiguration>
For the generalized format of a lifecycle configuration file, see the lifecycle configuration format for XML.
Remove the lifecycle configuration
When set on a bucket, the following lifecycle configuration disables lifecycle management by removing all rules:
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
In the list of buckets, click the name of the bucket for which you want to remove lifecycle rules.
Click the Lifecycle tab.
Click Delete all, or delete individual rules by clicking on the Trash icon associated with the rule you want to delete.
Command line
Use the gcloud storage buckets update
command with the
--clear-lifecycle
flag:
gcloud storage buckets update gs://BUCKET_NAME --clear-lifecycle
Where BUCKET_NAME is the name of the bucket whose lifecycle configuration you want to remove.
REST APIs
JSON API
{ "lifecycle": { "rule": [] } }
For the generalized format of a lifecycle configuration file, see the bucket resource representation for JSON.
XML API
<?xml version="1.0" encoding="UTF-8" ?> <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.
- Set and view the lifecycle configuration on a bucket.