删除存储桶

本页面展示如何删除 Cloud Storage 存储桶

准备工作

如需获得删除 Cloud Storage 存储桶所需的权限,请让您的管理员为您授予存储桶的 Storage Admin (roles/storage.admin) IAM 角色。

此预定义角色可提供删除存储桶所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

  • storage.buckets.delete
  • storage.buckets.list
    • 只有使用 Google Cloud 控制台删除存储桶时,才需要此权限。
  • storage.objects.delete
    • 只有对象存在于您要删除的存储桶中时,才需要此权限。
  • storage.objects.list
    • 只有使用 Google Cloud 控制台或 Google Cloud CLI 删除存储桶时,才需要此权限。

您也可以使用其他自定义角色预定义角色来获得这些权限。

如需了解如何授予存储桶的角色,请参阅将 IAM 与存储桶搭配使用

删除存储桶

控制台

  1. 在 Google Cloud 控制台中,进入 Cloud Storage 存储桶页面。

    进入“存储桶”

  2. 选中与要删除的存储桶对应的复选框。

  3. 点击删除

  4. 在出现的叠加窗口中,确认要删除存储桶及其内容。

  5. 点击删除

如需了解如何在 Google Cloud 控制台中获取失败的 Cloud Storage 操作的详细错误信息,请参阅问题排查

命令行

  • 如需删除存储桶以及存储桶中的所有对象,请使用带有 --recursive 标志的 Google Cloud CLI 命令 gcloud storage rm

    gcloud storage rm --recursive gs://BUCKET_NAME

    其中 BUCKET_NAME 是要删除的存储桶的名称,例如 my-bucket

    如果成功,则响应类似如下示例:

    Removing gs://my-bucket/...
  • 如果存储桶包含托管文件夹,您可以使用带有 --recursive 标志的 Google Cloud CLI 命令 gcloud alpha storage rm 删除存储桶以及其中的所有托管文件夹和对象:

    gcloud alpha storage rm --recursive gs://BUCKET_NAME

    其中 BUCKET_NAME 是要删除的存储桶的名称,例如 my-bucket

    如果成功,则响应类似如下示例:

    Removing gs://my-bucket/...

如果要避免意外删除对象或托管文件夹,请勿在命令中使用 --recursive 标志。如果您不添加该标志,则该命令只会删除空的存储桶。

客户端库

C++

如需了解详情,请参阅 Cloud Storage C++ API 参考文档

如需向 Cloud Storage 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

namespace gcs = ::google::cloud::storage;
[](gcs::Client client, std::string const& bucket_name) {
  google::cloud::Status status = client.DeleteBucket(bucket_name);
  if (!status.ok()) throw std::runtime_error(status.message());

  std::cout << "The bucket " << bucket_name << " was deleted successfully.\n";
}

C#

如需了解详情,请参阅 Cloud Storage C# API 参考文档

如需向 Cloud Storage 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


using Google.Cloud.Storage.V1;
using System;

public class DeleteBucketSample
{
    public void DeleteBucket(string bucketName = "your-unique-bucket-name")
    {
        var storage = StorageClient.Create();
        storage.DeleteBucket(bucketName);
        Console.WriteLine($"The bucket {bucketName} was deleted.");
    }
}

Go

如需了解详情,请参阅 Cloud Storage Go API 参考文档

如需向 Cloud Storage 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

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

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

// deleteBucket deletes the bucket.
func deleteBucket(w io.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*30)
	defer cancel()

	bucket := client.Bucket(bucketName)
	if err := bucket.Delete(ctx); err != nil {
		return fmt.Errorf("Bucket(%q).Delete: %w", bucketName, err)
	}
	fmt.Fprintf(w, "Bucket %v deleted\n", bucketName)
	return nil
}

Java

如需了解详情,请参阅 Cloud Storage Java API 参考文档

如需向 Cloud Storage 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

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

    // The ID of the bucket to delete
    // String bucketName = "your-unique-bucket-name";

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Bucket bucket = storage.get(bucketName);
    bucket.delete();

    System.out.println("Bucket " + bucket.getName() + " was deleted");
  }
}

Node.js

如需了解详情,请参阅 Cloud Storage Node.js API 参考文档

如需向 Cloud Storage 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

/**
 * 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('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function deleteBucket() {
  await storage.bucket(bucketName).delete();
  console.log(`Bucket ${bucketName} deleted`);
}

deleteBucket().catch(console.error);

PHP

如需了解详情,请参阅 Cloud Storage PHP API 参考文档

如需向 Cloud Storage 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

use Google\Cloud\Storage\StorageClient;

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

Python

如需了解详情,请参阅 Cloud Storage Python API 参考文档

如需向 Cloud Storage 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

from google.cloud import storage


def delete_bucket(bucket_name):
    """Deletes a bucket. The bucket must be empty."""
    # bucket_name = "your-bucket-name"

    storage_client = storage.Client()

    bucket = storage_client.get_bucket(bucket_name)
    bucket.delete()

    print(f"Bucket {bucket.name} deleted")

Ruby

如需了解详情,请参阅 Cloud Storage Ruby API 参考文档

如需向 Cloud Storage 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

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

  require "google/cloud/storage"

  storage = Google::Cloud::Storage.new
  bucket  = storage.bucket bucket_name, skip_lookup: true

  bucket.delete

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

REST API

JSON API

  1. OAuth 2.0 Playground 获取授权访问令牌。将 Playground 配置为使用您自己的 OAuth 凭据。如需了解相关说明,请参阅 API 身份验证
  2. 使用 cURL,通过 DELETE Bucket 请求调用 JSON API

    curl -X DELETE -H "Authorization: Bearer OAUTH2_TOKEN" \
      "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME"

    其中:

    • OAUTH2_TOKEN 是您在第 1 步中生成的访问令牌。
    • BUCKET_NAME 是要删除的存储桶的名称,例如 my-bucket

如果成功,响应将包含 204 状态代码。

XML API

  1. OAuth 2.0 Playground 获取授权访问令牌。将 Playground 配置为使用您自己的 OAuth 凭据。如需了解相关说明,请参阅 API 身份验证
  2. 使用 cURL,通过 DELETE Bucket 请求调用 XML API

    curl -X DELETE -H "Authorization: Bearer OAUTH2_TOKEN" \
      "https://storage.googleapis.com/BUCKET_NAME"

    其中:

    • OAUTH2_TOKEN 是您在第 1 步中生成的访问令牌。
    • BUCKET_NAME 是要删除的存储桶的名称,例如 my-bucket

后续步骤