This page shows you how to set object lifecycle policy for Google Distributed Cloud (GDC) air-gapped storage buckets.

## Before you begin

Storage buckets are resources scoped to a specific [project](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/platform/pa-user/project-management.md.txt). The project acts as a container, grouping your buckets and enabling their management through the Management API server. Before you can perform the operations on this page, you must either create a new project or select an existing one where you have sufficient permissions.

### Required IAM roles

Contact your IAM Project Admin to request the following roles in your project
namespace:

- **Bucket Project Admin** (`project-bucket-admin`): create, update, and delete storage buckets.
- **Bucket Object Project Viewer** (`project-bucket-object-viewer`): view and check details of storage buckets and objects.

For more information, see [Grant bucket access](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/application/ao-user/grant-obtain-storage-access.md.txt#grant_bucket_access).

### Prepare your environment

To run commands against the management API server or the global API server, do the following:

1. [Sign in and generate](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/application/ao-user/iam/sign-in.md.txt#zonal-resources-kubeconfig) the kubeconfig file for the zonal management API server if you don't have one.
2. For dual-zone buckets, do the following:
   - Verify with your Infrastructure Operator (IO) that the `BucketLocationConfig` custom resource has been created for your paired zones.
   - [Generate the kubeconfig file](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/application/ao-user/iam/sign-in.md.txt#global-resources-kubeconfig) for the global API server.

## Set object lifecycle policy on bucket

You can use object lifecycle policy to automatically manage objects in
your buckets. This helps you store data more efficiently throughout its
lifespan. A lifecycle configuration is a list of rules to expire a
group of objects based on certain matching criteria.

You can create a bucket with or without object lifecycle policy. And you can apply new, update existing or remove existing lifecycle policy
after the bucket is created.

For unversioned buckets, expiration means permanently removing the object
asynchronously.

For versioned buckets, if the current object version is not a deletion marker,
expiration makes the current version noncurrent and then adds a delete marker to the current version.

To set and modify the object lifecycle policy,
update the `Bucket.spec.bucketPolicy.lifecyclePolicy` field.

### CLI


The following are few examples of bucket resources with different object
lifecycle policy configured:

```yaml
apiVersion: object.gdc.goog/v1
kind: Bucket
metadata:
  name: BUCKET_NAME
  namespace: NAMESPACE_NAME
spec:
  description: "Bucket with lifecycle rule to expire all objects after 365 days"
  storageClass: Standard
  bucketPolicy:
    lifecyclePolicy:
      enable: true
      lifecycleRules:
      - expiration:
          days: 365
        id: expire-all-after-365-days
        status: Enabled
---
apiVersion: object.gdc.goog/v1
kind: Bucket
metadata:
  name: BUCKET_NAME
  namespace: NAMESPACE_NAME
spec:
  description: "Bucket with lifecycle rule to expire objects in the logs/ directory after 30 days"
  storageClass: Standard
  bucketPolicy:
    lifecyclePolicy:
      enable: true
      lifecycleRules:
      - expiration:
          days: 30
        id: expire-logs-dir-after-30-days
        status: Enabled
        filter:
          prefixFilter: "logs/"
---
apiVersion: object.gdc.goog/v1
kind: Bucket
metadata:
  name: BUCKET_NAME
  namespace: NAMESPACE_NAME
spec:
  description: "Bucket with lifecycle rule to expire objects having specific tag after 30 days"
  storageClass: Standard
  bucketPolicy:
    lifecyclePolicy:
      enable: true
      lifecycleRules:
      - expiration:
          days: 30
        id: expire-tagged-objects-after-30-days
        status: Enabled
        filter:
          tagFilters:
          - key: environment
            value: dev
---
apiVersion: object.gdc.goog/v1
kind: Bucket
metadata:
  name: BUCKET_NAME
  namespace: NAMESPACE_NAME
spec:
  description: "Bucket with lifecycle rule to expire noncurrent versions after 90 days"
  storageClass: Standard
  bucketPolicy:
    lifecyclePolicy:
      enable: true
      lifecycleRules:
      - noncurrentExpiration:
          noncurrentDays: 90
        id: expire-noncurrent-after-90-days
        status: Enabled
---
apiVersion: object.gdc.goog/v1
kind: Bucket
metadata:
  name: BUCKET_NAME
  namespace: NAMESPACE_NAME
spec:
  description: "Bucket with lifecycle rule to expire objects on a specific date"
  storageClass: Standard
  bucketPolicy:
    lifecyclePolicy:
      enable: true
      lifecycleRules:
      - expiration:
          date: "2027-01-01T00:00:00Z"
        id: expire-on-specific-date
        status: Enabled
---
apiVersion: object.gdc.goog/v1
kind: Bucket
metadata:
  name: BUCKET_NAME
  namespace: NAMESPACE_NAME
spec:
  description: "Bucket with multiple lifecycle rules and advanced filters"
  storageClass: Standard
  bucketPolicy:
    lifecyclePolicy:
      enable: true
      lifecycleRules:
      # Rule 1: Expire objects in 'tmp/' directory AND tagged with 'status=ready' after 7 days
      - expiration:
          days: 7
        id: expire-ready-tmp-objects
        status: Enabled
        filter:
          prefixFilter: "tmp/"
          tagFilters:
          - key: status
            value: ready
      # Rule 2: Expire all objects in 'logs/' directory after 90 days
      - expiration:
          days: 90
        id: expire-logs-after-90-days
        status: Enabled
        filter:
          prefixFilter: "logs/"
```

> [!NOTE]
> **Note:** To expire noncurrent versions in a versioned bucket, use the `noncurrentExpiration` field as shown in the examples. You can combine both `expiration` and `noncurrentExpiration` in a single rule. If multiple filters (e.g. prefix and tags) are specified in a single rule, they are evaluated using AND logic (all conditions must match).

Any updates to the lifecycle policy applies to the existing objects and
objects uploaded after the update.

When the bucket also has object locking configured, objects are being
protected from expiration before the retention days end.