Managing Lifecycles

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

The following tasks show how to work with object lifecycle management using the gsutil tool, the JSON API, and the XML API. Support for this feature through the Google Cloud Client Libraries is coming soon. Support for this feature through the Google API Client Libraries is currently available.

Enabling Lifecycle Management

To enable lifecycle management for a bucket:

gsutil

  1. Create a .json file with the lifecycle configuration rules you would like to apply (see examples below).

  2. Use the lifecycle set command to apply the configuration, replacing [VALUES_IN_BRACKETS] with the appropriate values:

    gsutil lifecycle set [LIFECYCLE_CONFIG_FILE] gs://[BUCKET_NAME]

REST APIs

JSON API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Create a .json file with the lifecycle configuration rules you would like to apply (see examples below).
  3. Use cURL to call the JSON API with a PATCH Bucket request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PATCH --data-binary @[LIFECYCLE_CONFIG_FILE].json \
      -H "Authorization: Bearer [OAUTH2_TOKEN]" \
      -H "Content-Type: application/json" \
      "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=lifecycle"

XML API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Create a request body that contains the lifecycle configuration rules you would like to apply (see examples below).
  3. Use cURL to call the XML API with a PUT Bucket request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PUT --data-binary @[XML_FILE].xml \
      -H "Authorization: Bearer [OAUTH2_TOKEN]" \
      "https://[BUCKET_NAME].storage.googleapis.com/?lifecycle"

Disabling Lifecycle Management

To disable lifecycle management for a bucket:

gsutil

  1. Create a .json file with an empty lifecycle configuration:

    {}

  2. Use the lifecycle set command, replacing [VALUES_IN_BRACKETS] with the appropriate values:

    gsutil lifecycle set [LIFECYCLE_CONFIG_FILE] gs://[BUCKET_NAME]

REST APIs

JSON API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Create a .json file with an empty lifecycle configuration:
    {}
  3. Use cURL to call the JSON API with a PATCH Bucket request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PATCH --data-binary @[LIFECYCLE_CONFIG_FILE].json \
      -H "Authorization: Bearer [OAUTH2_TOKEN]" \
      -H "Content-Type: application/json" \
      "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=lifecycle"

XML API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Create a request body that contains an empty lifecycle configuration:
    <?xml version="1.0" encoding="UTF-8" ?>
    <LifecycleConfiguration/>
  3. Use cURL to call the XML API with a PUT Bucket request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PUT --data-binary @[XML_FILE].xml \
      -H "Authorization: Bearer [OAUTH2_TOKEN]" \
      "https://[BUCKET_NAME].storage.googleapis.com/?lifecycle"

Checking Lifecycle Configuration

To check the lifecycle configuration set on a bucket:

gsutil

  1. Use the lifecycle get command, replacing [VALUES_IN_BRACKETS] with the appropriate values::

    gsutil lifecycle get gs://[BUCKET_NAME]

REST APIs

JSON API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials. .
  2. Use cURL to call the JSON API with a GET Bucket request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X GET -H "Authorization: Bearer [OAUTH2_TOKEN]" \
      "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=lifecycle"

XML API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the XML API with a GET Bucket request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X GET \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://[BUCKET_NAME].storage.googleapis.com/?lifecycle"

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. For the generalized format of a lifecycle configuration file, see the bucket resource representation for JSON and the lifecycle configuration format for XML.

Delete an object

The following lifecycle configuration defines two rules. The first rule deletes any live objects older than 30 days. The second rule to deletes archived objects that have at least 3 newer versions (including the live version), which is equivalent to keeping only the 3 most recent versions. Note that the second rule requires object versioning to be enabled in order for its criteria to ever be met.

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": {
      "isLive": false,
      "numNewerVersions": 3
    }
  }
]
}
}

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": {
      "isLive": false,
      "numNewerVersions": 3
    }
  }
]
}
}

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>
            <NumberOfNewerVersions>3</NumberOfNewerVersions>
        </Condition>
    </Rule>
</LifecycleConfiguration>

Change an object's storage class

The following lifecycle configuration defines two rules to change the storage class of objects in a bucket. The first rule changes Multi-Regional Storage, Standard Storage, and Durable Reduced Availability Storage objects older than 365 days (one year) to Nearline Storage. The second rule changes Nearline Storage objects older than 1095 days (three years) to Coldline Storage. This lifecycle configuration applies to objects in the bucket regardless of when they were added.

Note that this lifecycle configuration applies to buckets in multi-regional locations. For buckets in regional locations, replace "MULTI_REGIONAL" with "REGIONAL".

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"]
    }
  }
]
}
}

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"]
    }
  }
]
}
}

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>

Send feedback about...

Cloud Storage Documentation