Deleting objects

This page shows you how to delete objects from your buckets in Cloud Storage. For an overview of objects, read the Key Terms.

To delete an object from one of your Cloud Storage buckets:

Console

  1. Open the Cloud Storage browser in the Google Cloud Console.
    Open the Cloud Storage browser
  2. In the list of buckets, click on the name of the bucket that contains the object you want to delete.

    The Bucket details page opens, with the Objects tab selected.

  3. Navigate to the object, which may be located in a folder.

  4. Click the checkbox next to the object you want to delete.

  5. Click the Delete button.

  6. Click Delete in the dialog that appears.

gsutil

Use the gsutil rm command:

gsutil rm gs://[BUCKET_NAME]/[OBJECT_NAME]

Where:

  • [BUCKET_NAME] is the name of the bucket containing the object you want to delete. For example, my-bucket.
  • [OBJECT_NAME] is the name of the object you want to delete. For example, pets/dog.png.

If successful, the response looks like the following example:

Operation completed over 1 objects.

Code samples

C++

For more information, see the Cloud Storage C++ API reference documentation.

namespace gcs = google::cloud::storage;
[](gcs::Client client, std::string const& bucket_name,
   std::string const& object_name) {
  google::cloud::Status status =
      client.DeleteObject(bucket_name, object_name);

  if (!status.ok()) throw std::runtime_error(status.message());
  std::cout << "Deleted " << object_name << " in bucket " << bucket_name
            << "\n";
}

C#

For more information, see the Cloud Storage C# API reference documentation.

private void DeleteObject(string bucketName, IEnumerable<string> objectNames)
{
    var storage = StorageClient.Create();
    foreach (string objectName in objectNames)
    {
        storage.DeleteObject(bucketName, objectName);
        Console.WriteLine($"Deleted {objectName}.");
    }
}

Go

For more information, see the Cloud Storage Go API reference documentation.

ctx := context.Background()

ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
o := client.Bucket(bucket).Object(object)
if err := o.Delete(ctx); err != nil {
	return err
}

Java

For more information, see the Cloud Storage Java API reference documentation.

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

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

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

    // The ID of your GCS object
    // String objectName = "your-object-name";

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    storage.delete(bucketName, objectName);

    System.out.println("Object " + objectName + " was deleted from " + bucketName);
  }
}

Node.js

For more information, see the Cloud Storage Node.js API reference documentation.

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'File to delete, e.g. file.txt';

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

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

async function deleteFile() {
  // Deletes the file from the bucket
  await storage.bucket(bucketName).file(filename).delete();

  console.log(`gs://${bucketName}/${filename} deleted.`);
}

deleteFile().catch(console.error);

PHP

For more information, see the Cloud Storage PHP API reference documentation.

use Google\Cloud\Storage\StorageClient;

/**
 * Delete an object.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $objectName the name of your Cloud Storage object.
 * @param array $options
 *
 * @return void
 */
function delete_object($bucketName, $objectName, $options = [])
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->object($objectName);
    $object->delete();
    printf('Deleted gs://%s/%s' . PHP_EOL, $bucketName, $objectName);
}

Python

For more information, see the Cloud Storage Python API reference documentation.

from google.cloud import storage


def delete_blob(bucket_name, blob_name):
    """Deletes a blob from the bucket."""
    # bucket_name = "your-bucket-name"
    # blob_name = "your-object-name"

    storage_client = storage.Client()

    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(blob_name)
    blob.delete()

    print("Blob {} deleted.".format(blob_name))

Ruby

For more information, see the Cloud Storage Ruby API reference documentation.

# bucket_name = "Your Google Cloud Storage bucket name"
# file_name   = "Name of file in Google Cloud Storage to delete"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new
bucket  = storage.bucket bucket_name
file    = bucket.file file_name

file.delete

puts "Deleted #{file.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. Use cURL to call the JSON API with a DELETE request:

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

    Where:

    • [OAUTH2_TOKEN] is the name of the access token you generated in Step 1.
    • [BUCKET_NAME] is the name of the bucket containing the object you want to delete. For example, my-bucket.
    • [OBJECT_NAME] is the name of the object you want to delete. For example, pets/dog.png.

XML API

  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 with a DELETE Object request:

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

    Where:

    • [OAUTH2_TOKEN] is the name of the access token you generated in Step 1.
    • [BUCKET_NAME] is the name of the bucket containing the object you want to delete. For example, my-bucket.
    • [OBJECT_NAME] is the name of the object you want to delete. For example, pets/dog.png.

What's next