Delete a bucket
Stay organized with collections
Save and categorize content based on your preferences.
Demonstrates deleting a Cloud Storage bucket.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["Demonstrates deleting a Cloud Storage bucket.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Delete buckets](/storage/docs/deleting-buckets)\n\nCode sample \n\nC++\n\n\nFor more information, see the\n[Cloud Storage C++ API\nreference documentation](/cpp/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n namespace gcs = ::google::cloud::storage;\n [](gcs::Client client, std::string const& bucket_name) {\n google::cloud::Status status = client.DeleteBucket(bucket_name);\n if (!status.ok()) throw std::runtime_error(status.message());\n\n std::cout \u003c\u003c \"The bucket \" \u003c\u003c bucket_name \u003c\u003c \" was deleted successfully.\\n\";\n }\n\nC#\n\n\nFor more information, see the\n[Cloud Storage C# API\nreference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n\n using https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;\n using System;\n\n public class DeleteBucketSample\n {\n public void DeleteBucket(string bucketName = \"your-unique-bucket-name\")\n {\n var storage = https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();\n storage.DeleteBucket(bucketName);\n Console.WriteLine($\"The bucket {bucketName} was deleted.\");\n }\n }\n\nGo\n\n\nFor more information, see the\n[Cloud Storage Go API\nreference documentation](https://pkg.go.dev/cloud.google.com/go/storage).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n \t\"time\"\n\n \t\"cloud.google.com/go/storage\"\n )\n\n // deleteBucket deletes the bucket.\n func deleteBucket(w io.https://cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Writer, bucketName string) error {\n \t// bucketName := \"bucket-name\"\n \tctx := context.Background()\n \tclient, err := storage.NewClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"storage.NewClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tctx, cancel := context.WithTimeout(ctx, time.Second*30)\n \tdefer cancel()\n\n \tbucket := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucketName)\n \tif err := bucket.Delete(ctx); err != nil {\n \t\treturn fmt.Errorf(\"Bucket(%q).Delete: %w\", bucketName, err)\n \t}\n \tfmt.Fprintf(w, \"Bucket %v deleted\\n\", bucketName)\n \treturn nil\n }\n\nJava\n\n\nFor more information, see the\n[Cloud Storage Java API\nreference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n import com.google.cloud.storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html;\n import com.google.cloud.storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;\n import com.google.cloud.storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;\n\n public class DeleteBucket {\n public static void deleteBucket(String projectId, String bucketName) {\n // The ID of your GCP project\n // String projectId = \"your-project-id\";\n\n // The ID of the bucket to delete\n // String bucketName = \"your-unique-bucket-name\";\n\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().setProjectId(projectId).build().https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html bucket = storage.https://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);\n bucket.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html#com_google_cloud_storage_Bucket_delete_com_google_cloud_storage_Bucket_BucketSourceOption____();\n\n System.out.println(\"Bucket \" + bucket.getName() + \" was deleted\");\n }\n }\n\nNode.js\n\n\nFor more information, see the\n[Cloud Storage Node.js API\nreference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample.\n */\n // The ID of your GCS bucket\n // const bucketName = 'your-unique-bucket-name';\n\n // Imports the Google Cloud client library\n const {Storage} = require('https://cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');\n\n // Creates a client\n const storage = new Storage();\n\n async function deleteBucket() {\n await storage.bucket(bucketName).delete();\n console.log(`Bucket ${bucketName} deleted`);\n }\n\n deleteBucket().catch(console.error);\n\nPHP\n\n\nFor more information, see the\n[Cloud Storage PHP API\nreference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n use Google\\Cloud\\Storage\\StorageClient;\n\n /**\n * Delete a Cloud Storage Bucket.\n *\n * @param string $bucketName The name of your Cloud Storage bucket.\n * (e.g. 'my-bucket')\n */\n function delete_bucket(string $bucketName): void\n {\n $storage = new StorageClient();\n $bucket = $storage-\u003ebucket($bucketName);\n $bucket-\u003edelete();\n printf('Bucket deleted: %s' . PHP_EOL, $bucket-\u003ename());\n }\n\nPython\n\n\nFor more information, see the\n[Cloud Storage Python API\nreference documentation](https://cloud.google.com/python/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/storage/latest/\n\n\n def delete_bucket(bucket_name):\n \"\"\"Deletes a bucket. The bucket must be empty.\"\"\"\n # bucket_name = \"your-bucket-name\"\n\n storage_client = https://cloud.google.com/python/docs/reference/storage/latest/.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()\n\n bucket = storage_client.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_get_bucket(bucket_name)\n bucket.delete()\n\n print(f\"Bucket {bucket.name} deleted\")\n\nRuby\n\n\nFor more information, see the\n[Cloud Storage Ruby API\nreference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n def delete_bucket bucket_name:\n # The ID of your GCS bucket\n # bucket_name = \"your-unique-bucket-name\"\n\n require \"google/cloud/storage\"\n\n storage = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-storage-control-v2/latest/Google-Cloud-Storage.html.https://cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html\n bucket = storage.bucket bucket_name, skip_lookup: true\n\n bucket.delete\n\n puts \"Deleted bucket: #{bucket.name}\"\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=storage)."]]