删除存储桶

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

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

所需权限

控制台

如需使用 Google Cloud 控制台完成本指南,您必须拥有适当的 IAM 权限。如果您要访问的存储桶在不是由您创建的项目中,则可能需要项目所有者为您提供包含必要权限的角色。

如需查看特定操作所需权限的列表,请参阅 Google Cloud 控制台的 IAM 权限

如需查看相关角色的列表,请参阅 Cloud Storage 角色。或者,您也可以创建一个自定义角色,并为其提供具体受限的权限。

命令行

为使用命令行实用程序完成本指南,您必须拥有适当的 IAM 权限。如果您要访问的存储桶在不是由您创建的项目中,则可能需要项目所有者为您提供包含必要权限的角色。

如需查看特定操作所需权限的列表,请参阅 gsutil 命令的 IAM 权限

如需查看相关角色的列表,请参阅 Cloud Storage 角色。或者,您也可以创建一个自定义角色,并为其提供具体受限的权限。

客户端库

如需使用 Cloud Storage 客户端库完成本指南,您必须拥有适当的 IAM 权限。如果您要访问的存储桶在不是由您创建的项目中,则可能需要项目所有者为您提供具有必要权限的角色。

除非另有说明,否则客户端库请求通过 JSON API 发出,并且需要 JSON 方法的 IAM 权限中列出的权限。如需查看使用客户端库发出请求时调用了哪些 JSON API 方法,请记录原始请求

如需查看相关 IAM 角色的列表,请参阅 Cloud Storage 角色。或者,您也可以创建一个自定义角色,并为其提供具体受限的权限。

REST API

JSON API

如需使用 JSON API 完成本指南,您必须拥有适当的 IAM 权限。如果您要访问的存储桶在不是由您创建的项目中,则可能需要项目所有者为您提供包含必要权限的角色。

如需查看特定操作所需的权限列表,请参阅 JSON 方法的 IAM 权限

如需查看相关角色的列表,请参阅 Cloud Storage 角色。或者,您也可以创建一个自定义角色,并为其提供具体受限的权限。

删除存储桶

控制台

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

    进入“存储桶”

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

  3. 点击删除

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

  5. 点击删除

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

命令行

gcloud

使用带有 --recursive 标志的 Google Cloud CLI 命令 gcloud storage rm

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

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

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

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

gsutil

使用带有 -r 标志的 gsutil rm 命令:

gsutil rm -r gs://BUCKET_NAME

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

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

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

客户端库

C++

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

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 参考文档


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 参考文档

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: %v", 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: %v", bucketName, err)
	}
	fmt.Fprintf(w, "Bucket %v deleted\n", bucketName)
	return nil
}

Java

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

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 参考文档

/**
 * 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 参考文档

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 参考文档

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 参考文档

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

后续步骤