Managing Buckets

This page shows you how to change the default storage class and label metadata for your Cloud Storage buckets, as well as how to delete buckets. For an overview of buckets, read the Key Terms. To learn more about specific bucket properties, see Storage Classes, Bucket Locations, and Bucket Labels.

Managing a bucket's default storage class

To change the default storage class of an existing bucket:

Console

  1. Open the Cloud Storage browser in the Google Cloud Platform Console.
    Open the Cloud Storage browser

  2. In the bucket list, find the bucket you want to modify, and click its more options button (three vertical dots).

  3. Click Edit default storage class.

  4. In the overlay window, select the new default storage class you would like for your bucket.

    Note that based on the location of your bucket, not all storage classes are available.

    Edit bucket storage class.

  5. Click Save.

gsutil

Use the gsutil defstorageclass set command with the desired storage class, replacing [VALUES_IN_BRACKETS] with the appropriate values:

gsutil defstorageclass set [STORAGE_CLASS] gs://[BUCKET_NAME]

The response looks like the following example:

Setting default storage class to "[STORAGE_CLASS]" for bucket 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 that contains the following information, replacing [VALUES_IN_BRACKETS] with the appropriate values:
  3. {
      "storageClass": "[STORAGE_CLASS]"
    }
  4. Use cURL to call the JSON API, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PUT --data-binary @[JSON_FILE_NAME].json \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        -H "Content-Type: application/json" \
        "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=storageClass"

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 .xml file that contains the following information, replacing [VALUES_IN_BRACKETS] with the appropriate values:
  3. <StorageClass>[STORAGE_CLASS]</StorageClass>
  4. Use cURL to call the XML API, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PUT --data-binary @[XML_FILE_NAME].xml \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://storage.googleapis.com/[BUCKET_NAME]?storageClass"

Managing a bucket's labels

To add, edit or delete the labels applied to a bucket:

Console

  1. Open the Cloud Storage browser in the Google Cloud Platform Console.
    Open the Cloud Storage browser

  2. Select the checkbox of the bucket whose labels you wish to manage.

  3. In the side panel that appears, click the Labels tab.

  4. Manage the bucket labels as desired.

    1. To add a label, click the + Add label button and specify a new key:value pair.

    2. To modify a label, edit the desired text box in the Value column. Note that you should create a new label if you want to change the Key.

    3. To delete a label, click the X associated with it.

  5. Click Save.

gsutil

Use the -l flag in a label ch command. For example:

gsutil label ch -l "[KEY_1]:[VALUE_1]" gs://[BUCKET_NAME]/

Where [KEY_1] is the key name for your label, [VALUE_1] is the value for your label and [BUCKET_NAME] is the name of the bucket that the label applies to.

  • Use multiple -l flags to add/edit multiple key:value pairs in a single command.

  • Use the -d flag to delete labels that have the specified keys.

  • Use the label set command to replace all existing labels with new ones.

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 that contains the following information, replacing [VALUES_IN_BRACKETS] with the appropriate values:
  3. {
      "labels": {
        "[KEY_1]": "[VALUE_1]"
      }
    }
  4. 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 @[JSON_FILE_NAME].json \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        -H "Content-Type: application/json" \
        "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=labels"

You can add/edit multiple key:value pairs in a request, and to delete a label, set its value to null. For example:

{
  "labels": {
    "New_Label_Key": "New_Label_Value",
    "Existing_Label_Key": "Updated_Label_Value",
    "Old_Label_Key": null
  }
}

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 .xml file that contains the following information, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    <Tagging>
      <TagSet>
        <Tag>
          <Key>[KEY_1]</Key>
          <Value>[VALUE_1]</Value>
        </Tag>
      </TagSet>
    </Tagging>

    Note that you can add multiple <Tag> elements in a request.

  3. Use cURL to call the XML API, with a PUT Bucket request and tagging query string parameter, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PUT --data-binary @[XML_FILE_NAME].xml \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://storage.googleapis.com/[BUCKET_NAME]?tagging"

Deleting a bucket

To delete a bucket:

Console

  1. Open the Cloud Storage browser in the Google Cloud Platform Console.
    Open the Cloud Storage browser

  2. Select the checkbox of the bucket you want to delete.

  3. Click Delete.

  4. In the overlay window that appears, confirm you want to delete the bucket and its contents by clicking Delete.

gsutil

Use the gsutil rm command with the -r flag, replacing [VALUES_IN_BRACKETS] with the appropriate values:

gsutil rm -r gs://[BUCKET_NAME]

The response looks like the following example:

Removing gs://[BUCKET_NAME]/...

Client libraries

C#

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

private void DeleteBucket(string bucketName)
{
    var storage = StorageClient.Create();
    storage.DeleteBucket(bucketName);
    Console.WriteLine($"Deleted {bucketName}.");
}

Go

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

if err := client.Bucket(bucketName).Delete(ctx); err != nil {
	return err
}

Java

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

boolean deleted = bucket.delete(BucketSourceOption.metagenerationMatch());
if (deleted) {
  // the bucket was deleted
} else {
  // the bucket was not found
}

Node.js

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

// Imports the Google Cloud client library
const Storage = require('@google-cloud/storage');

// The name of the bucket to delete, e.g. "my-bucket"
// const bucketName = "my-bucket";

// Instantiates a client
const storage = Storage();

// Deletes the bucket
storage
  .bucket(bucketName)
  .delete()
  .then(() => {
    console.log(`Bucket ${bucketName} deleted.`);
  })
  .catch((err) => {
    console.error('ERROR:', err);
  });

PHP

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

use Google\Cloud\Storage\StorageClient;

/**
 * Delete a Cloud Storage Bucket.
 *
 * @param string $bucketName the name of the bucket to delete.
 *
 * @return void
 */
function delete_bucket($bucketName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $bucket->delete();
    printf('Bucket deleted: %s' . PHP_EOL, $bucket->name());
}

Python

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

def delete_bucket(bucket_name):
    """Deletes a bucket. The bucket must be empty."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    bucket.delete()
    print('Bucket {} deleted'.format(bucket.name))

Ruby

For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Name of your Google Cloud Storage bucket to delete"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project: project_id
bucket  = storage.bucket bucket_name

bucket.delete

puts "Deleted bucket: #{bucket.name}"

REST APIs

JSON API

Note that your bucket must be empty before you can delete it.

  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, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X DELETE -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]"

If successful, the response contains a 204 status code.

See the Buckets: delete JSON reference page for more information.

XML API

Note that your bucket must be empty before you can delete it.

  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, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X DELETE -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://storage.googleapis.com/[BUCKET_NAME]"

See the Delete Bucket XML reference page for more information.

Send feedback about...

Cloud Storage Documentation