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

gcloud

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.

gsutil

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 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 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.
  2. 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 set a configuration section.

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

    1. Select the Set storage class to Nearline action.
    2. Select the Age condition, and enter a value of 365.
    3. Select the Storage class matches condition, and choose the Standard, Multi-Regional, and Durable Reduced Availability options.
  2. To move objects to Coldline storage 1095 days after they were created:

    1. Select the Set storage class to Coldline action.
    2. Select the Age condition, and enter a value of 1095.
    3. Select the Storage class matches condition, and choose the Nearline option.

Command line

gcloud

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.

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.

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 bucket list, find the bucket you want to enable, and click on the bucket's name.

  3. Click on the Lifecycle tab.

  4. Click Delete all, or delete individual rules by clicking on the Trash icon associated with the rule you wish to delete.

Command line

gcloud

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.

gsutil

{
"lifecycle": {
  "rule": []
}
}

For the generalized format of a lifecycle configuration file, see the bucket resource representation for JSON.

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