Configuration examples for Object Lifecycle Management

Overview Setup

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:

  1. 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.

  2. 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.

  1. To delete noncurrent versions of objects if there is a newer version:

    1. Select the Delete object action.
    2. Select the Live state condition, and select the Noncurrent radio button.
    3. Select the Number of newer versions condition, and enter a value of 2.
  2. To delete noncurrent versions of objects after they're been noncurrent for 7 days:

    1. Select the Delete object action.
    2. 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, an isLive condition set to true, and an age 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 for daysSinceNoncurrent 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:

  1. 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.
  2. 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.

  1. To move the objects to Nearline storage 365 days after they were created:

    1. Select the Set storage class to Nearline action.
    2. Select the Object name matches suffix rule scope.
    3. In the field that appears, type .jpg, and press enter. Then type .png, and press enter again.
    4. Select the Age condition, and enter a value of 365.
    5. Select the Storage class matches condition, and choose the Standard option.
  2. To move the objects to Coldline storage 1095 days after they were created:

    1. Select the Set storage class to Coldline action.
    2. Select the Object name matches suffix rule scope.
    3. In the field that appears, type .jpg, and press enter. Then type .png, and press enter again.
    4. Select the Age condition, and enter a value of 1095.
    5. 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

  1. In the Google Cloud console, go to the Cloud Storage Buckets page.

    Go to Buckets

  2. In the list of buckets, click the name of the bucket for which you want to remove lifecycle rules.

  3. Click the Lifecycle tab.

  4. 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