Object Lifecycle Management helps you optimize storage costs and automate data
retention by setting lifecycle rules on a bucket. For example, you can
transition objects to colder storage classes or delete expired data.

You can configure and view lifecycle rules on a bucket. These rules apply to all
current and future objects in that bucket.

For background information, see [Object Lifecycle Management](https://docs.cloud.google.com/storage/docs/lifecycle).

## Required roles

To get the permissions that you need to set and manage Object Lifecycle
Management for a bucket, ask your administrator to grant you the Storage Admin
(`roles/storage.admin`) role on the bucket. This [predefined role](https://docs.cloud.google.com/iam/docs/roles-overview#predefined) contains
the permissions required to set and manage Object Lifecycle Management for a
bucket. To see the exact permissions required, expand the
**Required permissions** section:

#### Required permissions

- `storage.buckets.get`
- `storage.buckets.list`
  - This permission is only required if you plan on using the Google Cloud console to perform the instructions on this page.
- `storage.buckets.update`

You might also be able to get these permissions with [custom roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles).

For information about granting roles on buckets, see
[Set and manage IAM policies on buckets](https://docs.cloud.google.com/storage/docs/access-control/using-iam-permissions).

## Set the lifecycle configuration for a bucket

> [!WARNING]
> **Warning:** When you change your lifecycle configuration, Object Lifecycle Management may still perform actions based on the old configuration for up to 24 hours. For example, if you change an `Age` condition from 10 days to 20 days, an object that is 11 days old could be deleted by Object Lifecycle Management up to 24 hours later, based on the old configuration.

### Console

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

   [Go to Buckets](https://console.cloud.google.com/storage/browser)
2. In the bucket list, find the bucket you want to enable, and click
   the bucket's name.

3. Click the **Lifecycle** tab.

   The lifecycle rules page appears. From here you can edit or [delete](https://docs.cloud.google.com/storage/docs/lifecycle-configurations#disable-example)
   existing rules. To add a new rule:
4. Click **Add a rule**.

5. In the page that appears, specify a configuration.

   1. Select the [action](https://docs.cloud.google.com/storage/docs/lifecycle#actions) to take when an object meets the conditions.

   2. Click **Continue**.

   3. Select the [conditions](https://docs.cloud.google.com/storage/docs/lifecycle#conditions) under which an action is taken.

   4. Click **Continue**.

   5. Click **Create**.

To learn how to get detailed error information about failed Cloud Storage
operations in the Google Cloud console, see
[Troubleshooting](https://docs.cloud.google.com/storage/docs/troubleshooting#trouble-console).

### Command line

1. Create a JSON file with the [lifecycle configuration rules](https://docs.cloud.google.com/storage/docs/lifecycle#configuration) you
   would like to apply. See [configuration examples](https://docs.cloud.google.com/storage/docs/lifecycle-configurations) for sample
   JSON files.

2. Use the [`gcloud storage buckets update`](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/update) command with the
   `--lifecycle-file` flag:

   ```
   gcloud storage buckets update gs://BUCKET_NAME --lifecycle-file=LIFECYCLE_CONFIG_FILE
   ```

   Where:
   - `BUCKET_NAME` is the name of the relevant bucket. For example, `my-bucket`.
   - `LIFECYCLE_CONFIG_FILE` is the path for the JSON file that you created in Step 1.

### Client libraries


### C++


For more information, see the
[Cloud Storage C++ API
reference documentation](https://docs.cloud.google.com/cpp/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample sets a lifecycle configuration on a bucket:

    namespace gcs = ::google::cloud::storage;
    using ::google::cloud::StatusOr;
    [](gcs::Client client, std::string const& bucket_name) {
      gcs::BucketLifecycle bucket_lifecycle_rules = gcs::BucketLifecycle{
          {gcs::LifecycleRule(gcs::LifecycleRule::ConditionConjunction(
                                  gcs::LifecycleRule::MaxAge(30),
                                  gcs::LifecycleRule::IsLive(true)),
                              gcs::LifecycleRule::Delete())}};

      StatusOr<gcs::BucketMetadata> updated_metadata = client.PatchBucket(
          bucket_name,
          gcs::BucketMetadataPatchBuilder().SetLifecycle(bucket_lifecycle_rules));
      if (!updated_metadata) throw std::move(updated_metadata).status();

      if (!updated_metadata->has_lifecycle() ||
          updated_metadata->lifecycle().rule.empty()) {
        std::cout << "Bucket lifecycle management is not enabled for bucket "
                  << updated_metadata->name() << ".\n";
        return;
      }
      std::cout << "Successfully enabled bucket lifecycle management for bucket "
                << updated_metadata->name() << ".\n";
      std::cout << "The bucket lifecycle rules are";
      for (auto const& kv : updated_metadata->lifecycle().rule) {
        std::cout << "\n " << kv.condition() << ", " << kv.action();
      }
      std::cout << "\n";
    }

The following sample removes any existing lifecycle configuration from a bucket:

    namespace gcs = ::google::cloud::storage;
    using ::google::cloud::StatusOr;
    [](gcs::Client client, std::string const& bucket_name) {
      StatusOr<gcs::BucketMetadata> updated_metadata = client.PatchBucket(
          bucket_name, gcs::BucketMetadataPatchBuilder().ResetLifecycle());
      if (!updated_metadata) throw std::move(updated_metadata).status();

      std::cout << "Successfully disabled bucket lifecycle management for bucket "
                << updated_metadata->name() << ".\n";
    }

### C#


For more information, see the
[Cloud Storage C# API
reference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample sets a lifecycle configuration on a bucket:


    using Google.Apis.Storage.v1.Data;
    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;
    using System;
    using System.Collections.Generic;

    public class EnableBucketLifecycleManagementSample
    {
        public Bucket EnableBucketLifecycleManagement(string bucketName = "your-unique-bucket-name")
        {
            var storage = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();
            var bucket = storage.GetBucket(bucketName);
            bucket.Lifecycle = new Bucket.LifecycleData
            {
                Rule = new List<Bucket.LifecycleData.RuleData>
                {
                    new Bucket.LifecycleData.RuleData
                    {
                        Condition = new Bucket.LifecycleData.RuleData.ConditionData { Age = 100 },
                        Action = new Bucket.LifecycleData.RuleData.ActionData { Type = "Delete" }
                    }
                }
            };
            bucket = storage.UpdateBucket(bucket);

            Console.WriteLine($"Lifecycle management is enabled for bucket {bucketName} and the rules are:");
            foreach (var rule in bucket.Lifecycle.Rule)
            {
                Console.WriteLine("Action:");
                Console.WriteLine($"Type: {rule.Action.Type}");
                Console.WriteLine($"Storage Class: {rule.Action.StorageClass}");

                Console.WriteLine("Condition:");
                Console.WriteLine($"Age: \t{rule.Condition.Age}");
                Console.WriteLine($"Created Before: \t{rule.Condition.CreatedBefore}");
                Console.WriteLine($"Time Before: \t{rule.Condition.CustomTimeBefore}");
                Console.WriteLine($"Days Since Custom Time: \t{rule.Condition.DaysSinceCustomTime}");
                Console.WriteLine($"Days Since Non-current Time: \t{rule.Condition.DaysSinceNoncurrentTime}");
                Console.WriteLine($"IsLive: \t{rule.Condition.IsLive}");
                Console.WriteLine($"Storage Class: \t{rule.Condition.MatchesStorageClass}");
                Console.WriteLine($"Noncurrent Time Before: \t{rule.Condition.NoncurrentTimeBefore}");
                Console.WriteLine($"Newer Versions: \t{rule.Condition.NumNewerVersions}");
            }
            return bucket;
        }
    }

The following sample removes any existing lifecycle configuration from a bucket:


    using Google.Apis.Storage.v1.Data;
    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;
    using System;

    public class DisableBucketLifecycleManagementSample
    {
        public Bucket DisableBucketLifecycleManagement(string bucketName = "your-unique-bucket-name")
        {
            var storage = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();
            var bucket = storage.GetBucket(bucketName);
            bucket.Lifecycle = null;
            bucket = storage.UpdateBucket(bucket);

            Console.WriteLine($"Lifecycle management is disabled for bucket {bucketName}.");
            return bucket;
        }
    }

### Go


For more information, see the
[Cloud Storage Go API
reference documentation](https://pkg.go.dev/cloud.google.com/go/storage).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample sets a lifecycle configuration on a bucket:

    import (
    	"context"
    	"fmt"
    	"io"
    	"time"

    	"cloud.google.com/go/storage"
    )

    // enableBucketLifecycleManagement adds a lifecycle delete rule with the
    // condition that the object is 100 days old.
    func enableBucketLifecycleManagement(w io.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Writer, bucketName string) error {
    	// bucketName := "bucket-name"
    	ctx := context.Background()
    	client, err := storage.NewClient(ctx)
    	if err != nil {
    		return fmt.Errorf("storage.NewClient: %w", err)
    	}
    	defer client.Close()

    	ctx, cancel := context.WithTimeout(ctx, time.Second*10)
    	defer cancel()

    	bucket := client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucketName)
    	bucketAttrsToUpdate := storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_BucketAttrsToUpdate{
    		Lifecycle: &storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Lifecycle{
    			Rules: []storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_LifecycleRule{
    				{
    					Action: storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_LifecycleAction{Type: "Delete"},
    					Condition: storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_LifecycleCondition{
    						AgeInDays: 100,
    					},
    				},
    			},
    		},
    	}

    	attrs, err := bucket.Update(ctx, bucketAttrsToUpdate)
    	if err != nil {
    		return fmt.Errorf("Bucket(%q).Update: %w", bucketName, err)
    	}
    	fmt.Fprintf(w, "Lifecycle management is enabled for bucket %v\n and the rules are:\n", bucketName)
    	for _, rule := range attrs.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Lifecycle.Rules {
    		fmt.Fprintf(w, "Action: %v\n", rule.Action)
    		fmt.Fprintf(w, "Condition: %v\n", rule.Condition)
    	}

    	return nil
    }

The following sample removes any existing lifecycle configuration from a bucket:

    import (
    	"context"
    	"fmt"
    	"io"
    	"time"

    	"cloud.google.com/go/storage"
    )

    // disableBucketLifecycleManagement removes all existing lifecycle rules
    // from the bucket.
    func disableBucketLifecycleManagement(w io.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Writer, bucketName string) error {
    	// bucketName := "bucket-name"
    	ctx := context.Background()
    	client, err := storage.NewClient(ctx)
    	if err != nil {
    		return fmt.Errorf("storage.NewClient: %w", err)
    	}
    	defer client.Close()

    	ctx, cancel := context.WithTimeout(ctx, time.Second*10)
    	defer cancel()

    	bucket := client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucketName)
    	bucketAttrsToUpdate := storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_BucketAttrsToUpdate{
    		Lifecycle: &storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Lifecycle{},
    	}

    	_, err = bucket.Update(ctx, bucketAttrsToUpdate)
    	if err != nil {
    		return fmt.Errorf("Bucket(%q).Update: %w", bucketName, err)
    	}
    	fmt.Fprintf(w, "Lifecycle management is disabled for bucket %v.\n", bucketName)

    	return nil
    }

### Java


For more information, see the
[Cloud Storage Java API
reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample sets a lifecycle configuration on a bucket:

    import static com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.LifecycleRule.html.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleAction.html;
    import static com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.LifecycleRule.html.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleCondition.html;

    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.LifecycleRule.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;
    import com.google.common.collect.ImmutableList;

    public class EnableLifecycleManagement {
      public static void enableLifecycleManagement(String projectId, String bucketName) {
        // The ID of your GCP project
        // String projectId = "your-project-id";

        // The ID of your GCS bucket
        // String bucketName = "your-unique-bucket-name";

        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().setProjectId(projectId).build().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();
        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html bucket = storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_get_com_google_cloud_storage_BlobId_(bucketName);

        // See the LifecycleRule documentation for additional info on what you can do with lifecycle
        // management rules. This one deletes objects that are over 100 days old.
        // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/BucketInfo.LifecycleRule.html
        bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html#com_google_cloud_storage_Bucket_toBuilder__()
            .setLifecycleRules(
                ImmutableList.of(
                    new https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.LifecycleRule.html(
                        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleAction.html.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleAction.html#com_google_cloud_storage_BucketInfo_LifecycleRule_LifecycleAction_newDeleteAction__(),
                        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleCondition.html.newBuilder().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleCondition.Builder.html#com_google_cloud_storage_BucketInfo_LifecycleRule_LifecycleCondition_Builder_setAge_java_lang_Integer_(100).build())))
            .build()
            .update();

        System.out.println("Lifecycle management was enabled and configured for bucket " + bucketName);
      }
    }

The following sample removes any existing lifecycle configuration from a bucket:

    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.BucketTargetOption.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;

    public class DisableLifecycleManagement {
      public static void disableLifecycleManagement(String projectId, String bucketName) {
        // The ID of your GCP project
        // String projectId = "your-project-id";

        // The ID of your GCS bucket
        // String bucketName = "your-unique-bucket-name";

        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().setProjectId(projectId).build().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();
        // first look up the bucket, so we will have its metageneration
        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html bucket = storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_get_com_google_cloud_storage_BlobId_(bucketName);
        storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_update_com_google_cloud_storage_BlobInfo_(
            bucket.toBuilder().deleteLifecycleRules().build(),
            BucketTargetOption.metagenerationMatch());

        System.out.println("Lifecycle management was disabled for bucket " + bucketName);
      }
    }

### Node.js


For more information, see the
[Cloud Storage Node.js API
reference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample sets a lifecycle configuration on a bucket:

    /**
     * TODO(developer): Uncomment the following lines before running the sample.
     */
    // The ID of your GCS bucket
    // const bucketName = 'your-unique-bucket-name';

    // Imports the Google Cloud client library
    const {Storage} = require('https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');

    // Creates a client
    const storage = new https://docs.cloud.google.com/nodejs/docs/reference/storage-control/latest/storage-control/protos.google.storage.v2.storage-class.html();

    async function enableBucketLifecycleManagement() {
      const [metadata] = await storage.bucket(bucketName).https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/bucket.html({
        action: {
          type: 'Delete',
        },
        condition: {age: 100},
      });

      console.log(
        `Lifecycle management is enabled for bucket ${bucketName} and the rules are:`
      );

      console.log(metadata.lifecycle.rule);
    }

    enableBucketLifecycleManagement().catch(console.error);

The following sample removes any existing lifecycle configuration from a bucket:

    /**
     * TODO(developer): Uncomment the following lines before running the sample.
     */
    // The ID of your GCS bucket
    // const bucketName = 'your-unique-bucket-name';

    // Imports the Google Cloud client library
    const {Storage} = require('https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');

    // Creates a client
    const storage = new https://docs.cloud.google.com/nodejs/docs/reference/storage-control/latest/storage-control/protos.google.storage.v2.storage-class.html();

    async function disableBucketLifecycleManagement() {
      await storage.bucket(bucketName).setMetadata({lifecycle: null});

      console.log(`Lifecycle management is disabled for bucket ${bucketName}`);
    }

    disableBucketLifecycleManagement().catch(console.error);

### PHP


For more information, see the
[Cloud Storage PHP API
reference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample sets a lifecycle configuration on a bucket:

    use Google\Cloud\Storage\StorageClient;
    use Google\Cloud\Storage\Bucket;

    /**
     * Enable bucket lifecycle management.
     *
     * @param string $bucketName The name of your Cloud Storage bucket.
     *        (e.g. 'my-bucket')
     */
    function enable_bucket_lifecycle_management(string $bucketName): void
    {
        $storage = new StorageClient();
        $bucket = $storage->bucket($bucketName);

        $lifecycle = Bucket::lifecycle()
            ->addDeleteRule([
                'age' => 100
            ]);

        $bucket->update([
            'lifecycle' => $lifecycle
        ]);

        $lifecycle = $bucket->currentLifecycle();

        printf('Lifecycle management is enabled for bucket %s and the rules are:' . PHP_EOL, $bucketName);
        foreach ($lifecycle as $rule) {
            print_r($rule);
            print(PHP_EOL);
        }
    }

The following sample removes any existing lifecycle configuration from a bucket:

    use Google\Cloud\Storage\StorageClient;

    /**
     * Disable bucket lifecycle management.
     *
     * @param string $bucketName The name of your Cloud Storage bucket.
     *        (e.g. 'my-bucket')
     */
    function disable_bucket_lifecycle_management(string $bucketName): void
    {
        $storage = new StorageClient();

        $bucket = $storage->bucket($bucketName);

        $bucket->update([
            'lifecycle' => null
        ]);

        printf('Lifecycle management is disabled for bucket %s.' . PHP_EOL, $bucketName);
    }

### Python


For more information, see the
[Cloud Storage Python API
reference documentation](https://cloud.google.com/python/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample sets a lifecycle configuration on a bucket:

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/storage/latest


    def enable_bucket_lifecycle_management(bucket_name):
        """Enable lifecycle management for a bucket"""
        # bucket_name = "my-bucket"

        storage_client = https://docs.cloud.google.com/python/docs/reference/storage/latest.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()

        bucket = storage_client.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_get_bucket(bucket_name)
        rules = bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_lifecycle_rules

        print(f"Lifecycle management rules for bucket {bucket_name} are {list(rules)}")
        bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_add_lifecycle_delete_rule(age=2)
        bucket.patch()

        rules = bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_lifecycle_rules
        print(f"Lifecycle management is enable for bucket {bucket_name} and the rules are {list(rules)}")

        return bucket

The following sample removes any existing lifecycle configuration from a bucket:

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/storage/latest


    def disable_bucket_lifecycle_management(bucket_name):
        """Disable lifecycle management for a bucket"""
        # bucket_name = "my-bucket"

        storage_client = https://docs.cloud.google.com/python/docs/reference/storage/latest.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()

        bucket = storage_client.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_get_bucket(bucket_name)
        bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_clear_lifecyle_rules()
        bucket.patch()
        rules = bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_lifecycle_rules

        print(f"Lifecycle management is disable for bucket {bucket_name} and the rules are {list(rules)}")
        return bucket

### Ruby


For more information, see the
[Cloud Storage Ruby API
reference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample sets a lifecycle configuration on a bucket:

    def enable_bucket_lifecycle_management bucket_name:
      # Enable lifecycle management for a bucket
      # The ID of your GCS bucket
      # bucket_name = "your-unique-bucket-name"

      require "google/cloud/storage"

      storage = Google::Cloud::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage-control/latest/Google-Cloud-Storage.html.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html
      bucket = storage.bucket bucket_name

      rules = bucket.lifecycle do |l|
        l.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket-Lifecycle.html age: 2
      end

      puts "Lifecycle management is enabled for bucket #{bucket_name} and the rules are #{rules}"
    end

The following sample removes any existing lifecycle configuration from a bucket:

    def disable_bucket_lifecycle_management bucket_name:
      # Disable lifecycle management for a bucket
      # The ID of your GCS bucket
      # bucket_name = "your-unique-bucket-name"

      require "google/cloud/storage"

      storage = Google::Cloud::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage-control/latest/Google-Cloud-Storage.html.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html
      bucket = storage.bucket bucket_name

      bucket.lifecycle do |l|
        l.clear
      end

      puts "Lifecycle management is disabled for bucket #{bucket_name}"
    end

### Rust

The following sample sets a lifecycle configuration on a bucket:

    use google_cloud_storage::client::StorageControl;
    use google_cloud_storage::model::bucket::Lifecycle;
    use google_cloud_storage::model::bucket::lifecycle::{
        Rule,
        rule::{Action, Condition},
    };
    use google_cloud_wkt::FieldMask;

    pub async fn sample(client: &StorageControl, bucket_id: &str) -> anyhow::Result<()> {
        let bucket = client
            .get_bucket()
            .set_name(format!("projects/_/buckets/{bucket_id}"))
            .send()
            .await?;
        let metageneration = bucket.metageneration;
        let rule = Rule::new()
            .set_action(Action::new().set_type("Delete"))
            .set_condition(Condition::new().set_age_days(30));
        let lifecycle = Lifecycle::new().set_rule(vec![rule]);
        let bucket = client
            .update_bucket()
            .set_bucket(bucket.set_lifecycle(lifecycle))
            .set_if_metageneration_match(metageneration)
            .set_update_mask(FieldMask::default().set_paths(["lifecycle"]))
            .send()
            .await?;
        println!("Lifecycle management enabled for bucket {bucket_id}: {bucket:?}");
        Ok(())
    }

The following sample removes any existing lifecycle configuration from a bucket:

    use google_cloud_storage::client::StorageControl;
    use google_cloud_storage::model::bucket::Lifecycle;
    use google_cloud_wkt::FieldMask;

    pub async fn sample(client: &StorageControl, bucket_id: &str) -> anyhow::Result<()> {
        let bucket = client
            .get_bucket()
            .set_name(format!("projects/_/buckets/{bucket_id}"))
            .send()
            .await?;
        let metageneration = bucket.metageneration;
        let bucket = client
            .update_bucket()
            .set_bucket(bucket.set_lifecycle(Lifecycle::new()))
            .set_if_metageneration_match(metageneration)
            .set_update_mask(FieldMask::default().set_paths(["lifecycle"]))
            .send()
            .await?;
        println!("Lifecycle management disabled for bucket {bucket_id}: {bucket:?}");
        Ok(())
    }

<br />

### Terraform

You can use a [Terraform resource](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket) to set a lifecycle
configuration for a bucket managed by Terraform. To set a lifecycle
configuration on an existing bucket that isn't yet managed by Terraform,
you must first [import the existing bucket](https://docs.cloud.google.com/docs/terraform/resource-management/import).

To find out whether a lifecycle condition is supported by Terraform, see the
[Terraform documentation for the `condition` block](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket#condition).

    resource "random_id" "bucket_prefix" {
      byte_length = 8
    }

    resource "google_storage_bucket" "auto_expire" {
      provider = google-beta
      name     = "${random_id.bucket_prefix.hex}-example-bucket"
      location = "US"

      lifecycle_rule {
        condition {
          age = 3
        }
        action {
          type = "Delete"
        }
      }
    }

### REST APIs

### JSON API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. Create a JSON file with the [lifecycle configuration rules](https://docs.cloud.google.com/storage/docs/lifecycle#configuration) you
   would like to apply. See [configuration examples](https://docs.cloud.google.com/storage/docs/lifecycle-configurations) for sample
   JSON files.

3. Use [`cURL`](http://curl.haxx.se/) to call the [JSON API](https://docs.cloud.google.com/storage/docs/json_api) with a
   [`PATCH` Bucket](https://docs.cloud.google.com/storage/docs/json_api/v1/buckets/patch) request:

   ```
   curl -X PATCH --data-binary @LIFECYCLE_CONFIG_FILE \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "Content-Type: application/json" \
     "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME?fields=lifecycle"
   ```

   Where:
   - `LIFECYCLE_CONFIG_FILE` is the path for the JSON file that you created in Step 2.
   - `BUCKET_NAME` is the name of the relevant bucket. For example, `my-bucket`.

### XML API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. Create an XML file with the [lifecycle configuration rules](https://docs.cloud.google.com/storage/docs/lifecycle#configuration)
   you would like to apply. See [configuration examples](https://docs.cloud.google.com/storage/docs/lifecycle-configurations) for sample
   XML files.

3. Use [`cURL`](http://curl.haxx.se/) to call the [XML API](https://docs.cloud.google.com/storage/docs/xml-api/overview) with a
   [`PUT` Bucket](https://docs.cloud.google.com/storage/docs/xml-api/put-bucket-metadata) request and `lifecycle` query string parameter:

   ```
   curl -X PUT --data-binary @XML_FILE_NAME \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     "https://storage.googleapis.com/BUCKET_NAME?lifecycle"
   ```

   Where:
   - `XML_FILE_NAME` is the path for the XML file that you created in Step 2.
   - `BUCKET_NAME` is the name of the relevant bucket. For example, `my-bucket`.

> [!NOTE]
> **Note:** To remove the lifecycle configuration for a bucket, [set an empty lifecycle configuration](https://docs.cloud.google.com/storage/docs/lifecycle-configurations#disable-example).

## Check the lifecycle configuration for a bucket

### Console

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

   [Go to Buckets](https://console.cloud.google.com/storage/browser)
2. In the bucket list, the lifecycle status of each bucket is found
   in the **Lifecycle** column.

   You can click this status to add, view, edit, and delete rules.

To learn how to get detailed error information about failed Cloud Storage
operations in the Google Cloud console, see
[Troubleshooting](https://docs.cloud.google.com/storage/docs/troubleshooting#trouble-console).

### Command line

Use the [`gcloud storage buckets describe`](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/describe) command with the
`--format` flag:

```
gcloud storage buckets describe gs://BUCKET_NAME --format="default(lifecycle_config)"
```

Where `BUCKET_NAME` is the name of the bucket
whose lifecycle configuration you want to view. For example,
`my-bucket`.

### Client libraries


### C++


For more information, see the
[Cloud Storage C++ API
reference documentation](https://docs.cloud.google.com/cpp/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    namespace gcs = ::google::cloud::storage;
    using ::google::cloud::StatusOr;
    [](gcs::Client client, std::string const& bucket_name) {
      StatusOr<gcs::BucketMetadata> updated_metadata =
          client.GetBucketMetadata(bucket_name);
      if (!updated_metadata) throw std::move(updated_metadata).status();

      if (!updated_metadata->has_lifecycle() ||
          updated_metadata->lifecycle().rule.empty()) {
        std::cout << "Bucket lifecycle management is not enabled for bucket "
                  << updated_metadata->name() << ".\n";
        return;
      }
      std::cout << "Bucket lifecycle management is enabled for bucket "
                << updated_metadata->name() << ".\n";
      std::cout << "The bucket lifecycle rules are";
      for (auto const& kv : updated_metadata->lifecycle().rule) {
        std::cout << "\n " << kv.condition() << ", " << kv.action();
      }
      std::cout << "\n";
    }

### C#


For more information, see the
[Cloud Storage C# API
reference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

To view the bucket's lifecycle policies, follow the instructions for displaying a bucket's metadata and look for the lifecycle policy fields in the response.


    using Google.Apis.Storage.v1.Data;
    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;
    using System;

    public class GetBucketMetadataSample
    {
        public Bucket GetBucketMetadata(string bucketName = "your-unique-bucket-name")
        {
            var storage = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();
            var bucket = storage.GetBucket(bucketName, new https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.GetBucketOptions.html { Projection = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.Projection.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.Projection.html#Google_Cloud_Storage_V1_Projection_Full });
            Console.WriteLine($"Bucket:\t{bucket.Name}");
            Console.WriteLine($"Acl:\t{bucket.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.UrlSigner.PostPolicyStandardElement.html#Google_Cloud_Storage_V1_UrlSigner_PostPolicyStandardElement_Acl}");
            Console.WriteLine($"Billing:\t{bucket.Billing}");
            Console.WriteLine($"Cors:\t{bucket.Cors}");
            Console.WriteLine($"DefaultEventBasedHold:\t{bucket.DefaultEventBasedHold}");
            Console.WriteLine($"DefaultObjectAcl:\t{bucket.DefaultObjectAcl}");
            Console.WriteLine($"Encryption:\t{bucket.Encryption}");
            if (bucket.Encryption != null)
            {
                Console.WriteLine($"KmsKeyName:\t{bucket.Encryption.DefaultKmsKeyName}");
            }
            Console.WriteLine($"Id:\t{bucket.Id}");
            Console.WriteLine($"Kind:\t{bucket.Kind}");
            Console.WriteLine($"Lifecycle:\t{bucket.Lifecycle}");
            Console.WriteLine($"Location:\t{bucket.Location}");
            Console.WriteLine($"LocationType:\t{bucket.LocationType}");
            Console.WriteLine($"Logging:\t{bucket.Logging}");
            Console.WriteLine($"Metageneration:\t{bucket.Metageneration}");
            Console.WriteLine($"ObjectRetention:\t{bucket.ObjectRetention}");
            Console.WriteLine($"Owner:\t{bucket.Owner}");
            Console.WriteLine($"ProjectNumber:\t{bucket.ProjectNumber}");
            Console.WriteLine($"RetentionPolicy:\t{bucket.RetentionPolicy}");
            Console.WriteLine($"SelfLink:\t{bucket.SelfLink}");
            Console.WriteLine($"StorageClass:\t{bucket.StorageClass}");
            Console.WriteLine($"TimeCreated:\t{bucket.TimeCreated}");
            Console.WriteLine($"Updated:\t{bucket.Updated}");
            Console.WriteLine($"Versioning:\t{bucket.Versioning}");
            Console.WriteLine($"Website:\t{bucket.Website}");
            Console.WriteLine($"TurboReplication:\t{bucket.Rpo}");
            if (bucket.Labels != null)
            {
                Console.WriteLine("Labels:");
                foreach (var label in bucket.Labels)
                {
                    Console.WriteLine($"{label.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.UrlSigner.PostPolicyStandardElement.html#Google_Cloud_Storage_V1_UrlSigner_PostPolicyStandardElement_Key}:\t{label.Value}");
                }
            }
            return bucket;
        }
    }

### Go


For more information, see the
[Cloud Storage Go API
reference documentation](https://pkg.go.dev/cloud.google.com/go/storage).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

To view the bucket's lifecycle policies, follow the instructions for displaying a bucket's metadata and look for the lifecycle policy fields in the response.

    import (
    	"context"
    	"fmt"
    	"io"
    	"time"

    	"cloud.google.com/go/storage"
    )

    // getBucketMetadata gets the bucket metadata.
    func getBucketMetadata(w io.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Writer, bucketName string) (*storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_BucketAttrs, error) {
    	// bucketName := "bucket-name"
    	ctx := context.Background()
    	client, err := storage.NewClient(ctx)
    	if err != nil {
    		return nil, fmt.Errorf("storage.NewClient: %w", err)
    	}
    	defer client.Close()

    	ctx, cancel := context.WithTimeout(ctx, time.Second*10)
    	defer cancel()
    	attrs, err := client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucketName).Attrs(ctx)
    	if err != nil {
    		return nil, fmt.Errorf("Bucket(%q).Attrs: %w", bucketName, err)
    	}
    	fmt.Fprintf(w, "BucketName: %v\n", attrs.Name)
    	fmt.Fprintf(w, "Location: %v\n", attrs.Location)
    	fmt.Fprintf(w, "LocationType: %v\n", attrs.LocationType)
    	fmt.Fprintf(w, "StorageClass: %v\n", attrs.StorageClass)
    	fmt.Fprintf(w, "Turbo replication (RPO): %v\n", attrs.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_RPO)
    	fmt.Fprintf(w, "TimeCreated: %v\n", attrs.Created)
    	fmt.Fprintf(w, "Metageneration: %v\n", attrs.MetaGeneration)
    	fmt.Fprintf(w, "PredefinedACL: %v\n", attrs.PredefinedACL)
    	if attrs.Encryption != nil {
    		fmt.Fprintf(w, "DefaultKmsKeyName: %v\n", attrs.Encryption.DefaultKMSKeyName)
    	}
    	if attrs.Website != nil {
    		fmt.Fprintf(w, "IndexPage: %v\n", attrs.Website.MainPageSuffix)
    		fmt.Fprintf(w, "NotFoundPage: %v\n", attrs.Website.NotFoundPage)
    	}
    	fmt.Fprintf(w, "DefaultEventBasedHold: %v\n", attrs.DefaultEventBasedHold)
    	if attrs.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_RetentionPolicy != nil {
    		fmt.Fprintf(w, "RetentionEffectiveTime: %v\n", attrs.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_RetentionPolicy.EffectiveTime)
    		fmt.Fprintf(w, "RetentionPeriod: %v\n", attrs.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_RetentionPolicy.RetentionPeriod)
    		fmt.Fprintf(w, "RetentionPolicyIsLocked: %v\n", attrs.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_RetentionPolicy.IsLocked)
    	}
    	fmt.Fprintf(w, "ObjectRetentionMode: %v\n", attrs.ObjectRetentionMode)
    	fmt.Fprintf(w, "RequesterPays: %v\n", attrs.RequesterPays)
    	fmt.Fprintf(w, "VersioningEnabled: %v\n", attrs.VersioningEnabled)
    	if attrs.Logging != nil {
    		fmt.Fprintf(w, "LogBucket: %v\n", attrs.Logging.LogBucket)
    		fmt.Fprintf(w, "LogObjectPrefix: %v\n", attrs.Logging.LogObjectPrefix)
    	}
    	if attrs.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_CORS != nil {
    		fmt.Fprintln(w, "CORS:")
    		for _, v := range attrs.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_CORS {
    			fmt.Fprintf(w, "\tMaxAge: %v\n", v.MaxAge)
    			fmt.Fprintf(w, "\tMethods: %v\n", v.Methods)
    			fmt.Fprintf(w, "\tOrigins: %v\n", v.Origins)
    			fmt.Fprintf(w, "\tResponseHeaders: %v\n", v.ResponseHeaders)
    		}
    	}
    	if attrs.Labels != nil {
    		fmt.Fprintf(w, "\n\n\nLabels:")
    		for key, value := range attrs.Labels {
    			fmt.Fprintf(w, "\t%v = %v\n", key, value)
    		}
    	}
    	return attrs, nil
    }

### Java


For more information, see the
[Cloud Storage Java API
reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

To view the bucket's lifecycle policies, follow the instructions for displaying a bucket's metadata and look for the lifecycle policy fields in the response.


    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;
    import java.util.Map;

    public class GetBucketMetadata {
      public static void getBucketMetadata(String projectId, String bucketName) {
        // The ID of your GCP project
        // String projectId = "your-project-id";

        // The ID of your GCS bucket
        // String bucketName = "your-unique-bucket-name";

        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().setProjectId(projectId).build().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();

        // Select all fields. Fields can be selected individually e.g. Storage.BucketField.NAME
        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html bucket =
            storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_get_com_google_cloud_storage_BlobId_(bucketName, Storage.BucketGetOption.fields(Storage.BucketField.values()));

        // Print bucket metadata
        System.out.println("BucketName: " + bucket.getName());
        System.out.println("DefaultEventBasedHold: " + bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html#com_google_cloud_storage_BucketInfo_getDefaultEventBasedHold__());
        System.out.println("DefaultKmsKeyName: " + bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html#com_google_cloud_storage_BucketInfo_getDefaultKmsKeyName__());
        System.out.println("Id: " + bucket.getGeneratedId());
        System.out.println("IndexPage: " + bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html#com_google_cloud_storage_BucketInfo_getIndexPage__());
        System.out.println("Location: " + bucket.getLocation());
        System.out.println("LocationType: " + bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html#com_google_cloud_storage_BucketInfo_getLocationType__());
        System.out.println("Metageneration: " + bucket.getMetageneration());
        System.out.println("NotFoundPage: " + bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html#com_google_cloud_storage_BucketInfo_getNotFoundPage__());
        System.out.println("RetentionEffectiveTime: " + bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html#com_google_cloud_storage_BucketInfo_getRetentionEffectiveTime__());
        System.out.println("RetentionPeriod: " + bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html#com_google_cloud_storage_BucketInfo_getRetentionPeriod__());
        System.out.println("RetentionPolicyIsLocked: " + bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html#com_google_cloud_storage_BucketInfo_retentionPolicyIsLocked__());
        System.out.println("RequesterPays: " + bucket.requesterPays());
        System.out.println("SelfLink: " + bucket.getSelfLink());
        System.out.println("StorageClass: " + bucket.getStorageClass().https://docs.cloud.google.com/java/docs/reference/google-cloud-nio/latest/com.google.cloud.storage.contrib.nio.CloudStorageFileAttributeView.html#com_google_cloud_storage_contrib_nio_CloudStorageFileAttributeView_name__());
        System.out.println("TimeCreated: " + bucket.getCreateTime());
        System.out.println("VersioningEnabled: " + bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BucketInfo.html#com_google_cloud_storage_BucketInfo_versioningEnabled__());
        System.out.println("ObjectRetention: " + bucket.getObjectRetention());
        if (bucket.getLabels() != null) {
          System.out.println("\n\n\nLabels:");
          for (Map.Entry<String, String> label : bucket.getLabels().entrySet()) {
            System.out.println(label.getKey() + "=" + label.getValue());
          }
        }
        if (bucket.getLifecycleRules() != null) {
          System.out.println("\n\n\nLifecycle Rules:");
          for (BucketInfo.LifecycleRule rule : bucket.getLifecycleRules()) {
            System.out.println(rule);
          }
        }
      }
    }

### Node.js


For more information, see the
[Cloud Storage Node.js API
reference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

To view the bucket's lifecycle policies, follow the instructions for displaying a bucket's metadata and look for the lifecycle policy fields in the response.

    // Imports the Google Cloud client library
    const {Storage} = require('https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');

    // Creates a client
    const storage = new https://docs.cloud.google.com/nodejs/docs/reference/storage-control/latest/storage-control/protos.google.storage.v2.storage-class.html();

    async function getBucketMetadata() {
      /**
       * TODO(developer): Uncomment the following lines before running the sample.
       */
      // The ID of your GCS bucket
      // const bucketName = 'your-unique-bucket-name';

      // Get Bucket Metadata
      const [metadata] = await storage.bucket(bucketName).getMetadata();

      console.log(https://docs.cloud.google.com/nodejs/docs/reference/storage-transfer/latest/storage-transfer/protos.google.storagetransfer.v1.notificationconfig.payloadformat.html.stringify(metadata, null, 2));
    }

### PHP


For more information, see the
[Cloud Storage PHP API
reference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

To view the bucket's lifecycle policies, follow the instructions for displaying a bucket's metadata and look for the lifecycle policy fields in the response.

    use Google\Cloud\Storage\StorageClient;

    /**
     * Get bucket metadata.
     *
     * @param string $bucketName The name of your Cloud Storage bucket.
     *        (e.g. 'my-bucket')
     */
    function get_bucket_metadata(string $bucketName): void
    {
        $storage = new StorageClient();
        $bucket = $storage->bucket($bucketName);
        $info = $bucket->info();

        printf('Bucket Metadata: %s' . PHP_EOL, print_r($info, true));
    }

### Python


For more information, see the
[Cloud Storage Python API
reference documentation](https://cloud.google.com/python/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

To view the bucket's lifecycle policies, follow the instructions for displaying a bucket's metadata and look for the lifecycle policy fields in the response.


    from google.cloud import https://docs.cloud.google.com/python/docs/reference/storage/latest


    def bucket_metadata(bucket_name):
        """Prints out a bucket's metadata."""
        # bucket_name = 'your-bucket-name'

        storage_client = https://docs.cloud.google.com/python/docs/reference/storage/latest.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()
        bucket = storage_client.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_get_bucket(bucket_name)

        print(f"ID: {bucket.id}")
        print(f"Name: {bucket.name}")
        print(f"Storage Class: {bucket.storage_class}")
        print(f"Location: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_location}")
        print(f"Location Type: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_location_type}")
        print(f"Cors: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_cors}")
        print(f"Default Event Based Hold: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_default_event_based_hold}")
        print(f"Default KMS Key Name: {bucket.default_kms_key_name}")
        print(f"Metageneration: {bucket.metageneration}")
        print(
            f"Public Access Prevention: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_iam_configuration.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.IAMConfiguration.html#google_cloud_storage_bucket_IAMConfiguration_public_access_prevention}"
        )
        print(f"Retention Effective Time: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_retention_policy_effective_time}")
        print(f"Retention Period: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_retention_period}")
        print(f"Retention Policy Locked: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_retention_policy_locked}")
        print(f"Object Retention Mode: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_object_retention_mode}")
        print(f"Requester Pays: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_requester_pays}")
        print(f"Self Link: {bucket.self_link}")
        print(f"Time Created: {bucket.time_created}")
        print(f"Versioning Enabled: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_versioning_enabled}")
        print(f"Labels: {bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_labels}")

### Ruby


For more information, see the
[Cloud Storage Ruby API
reference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

To view the bucket's lifecycle policies, follow the instructions for displaying a bucket's metadata and look for the lifecycle policy fields in the response.

    def get_bucket_metadata bucket_name:
      # The ID of your GCS bucket
      # bucket_name = "your-unique-bucket-name"

      require "google/cloud/storage"

      storage = Google::Cloud::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage-control/latest/Google-Cloud-Storage.html.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html
      bucket  = storage.bucket bucket_name

      puts "ID:                       #{bucket.id}"
      puts "Name:                     #{bucket.name}"
      puts "Storage Class:            #{bucket.storage_class}"
      puts "Location:                 #{bucket.location}"
      puts "Location Type:            #{bucket.location_type}"
      puts "Cors:                     #{bucket.cors}"
      puts "Default Event Based Hold: #{bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html}"
      puts "Default KMS Key Name:     #{bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html}"
      puts "Logging Bucket:           #{bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html}"
      puts "Logging Prefix:           #{bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html}"
      puts "Metageneration:           #{bucket.metageneration}"
      puts "Retention Effective Time: #{bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html}"
      puts "Retention Period:         #{bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html}"
      puts "Retention Policy Locked:  #{bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html}"
      puts "Requester Pays:           #{bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html}"
      puts "Self Link:                #{bucket.api_url}"
      puts "Time Created:             #{bucket.created_at}"
      puts "Versioning Enabled:       #{bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html}"
      puts "Index Page:               #{bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html}"
      puts "Not Found Page:           #{bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html}"
      puts "Labels:"
      bucket.labels.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Policy-Bindings.html do |key, value|
        puts " - #{key} = #{value}"
      end
      puts "Lifecycle Rules:"
      bucket.lifecycle.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Policy-Bindings.html do |rule|
        puts "#{rule.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket-Lifecycle-Rule.html} - #{rule.storage_class} - #{rule.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket-Lifecycle-Rule.html} - #{rule.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket-Lifecycle-Rule.html}"
      end
    end

### Rust

    use google_cloud_storage::client::StorageControl;

    pub async fn sample(client: &StorageControl, bucket_id: &str) -> anyhow::Result<()> {
        let bucket = client
            .get_bucket()
            .set_name(format!("projects/_/buckets/{bucket_id}"))
            .send()
            .await?;
        println!(
            "Lifecycle rules for bucket {}: {:?}",
            bucket_id, bucket.lifecycle
        );
        Ok(())
    }

<br />

### REST APIs

### JSON API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. Use [`cURL`](http://curl.haxx.se/) to call the [JSON API](https://docs.cloud.google.com/storage/docs/json_api) with a
   [`GET` Bucket](https://docs.cloud.google.com/storage/docs/json_api/v1/buckets/get) request:

   ```
   curl -X GET \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME?fields=lifecycle"
   ```

   Where `BUCKET_NAME` is the name of the
   relevant bucket. For example, `my-bucket`.

### XML API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. Use [`cURL`](http://curl.haxx.se/) to call the [XML API](https://docs.cloud.google.com/storage/docs/xml-api/overview) with a
   [`GET` Bucket](https://docs.cloud.google.com/storage/docs/xml-api/get-bucket-metadata) request and a `lifecycle` query string parameter:

   ```
   curl -X GET \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     "https://storage.googleapis.com/BUCKET_NAME?lifecycle"
   ```

   Where `BUCKET_NAME` is the name of the
   relevant bucket. For example, `my-bucket`.

## What's next

- Learn more about [Object Lifecycle Management](https://docs.cloud.google.com/storage/docs/lifecycle), including the available lifecycle actions and lifecycle conditions.
- Explore [lifecycle configuration examples](https://docs.cloud.google.com/storage/docs/lifecycle-configurations).