About object deletion

Deleting objects that you no longer need helps you reduce storage costs, comply with data retention requirements, and keep your buckets organized. Depending on your scale and automation needs, you can choose from several methods to delete objects manually, programmatically, or through automated policies.

This page describes the methods available for deleting objects from your buckets and helps you choose a suitable method for your task.

For information about how to delete objects, see Delete objects.

Single-object deletion

Single-object deletion provides precise control over specific data. You typically delete single objects when you need to perform immediate, targeted updates to your bucket's data. The following are some of the common reasons for deleting single objects:

  • Correcting errors: Removing a file that was uploaded accidentally or a file that contains incorrect data.

  • Cleanup: Manually removing temporary files or logs after completing a task.

  • Application logic: Automatically removing a file when you perform an action in an application, such as deleting a profile photo.

  • Security and compliance: Deleting objects as part of removing sensitive information.

Tools to delete a single object

To delete a single object from your bucket, use any of the following tools. Each tool sends an individual DELETE request per object.

Tool Description
Google Cloud console Deletes an object through the Google Cloud console.
Google Cloud CLI Deletes an object using the gcloud storage rm command.
Client libraries Deletes an object programmatically using a supported language like C++, C#, Go, Java, Node.js, PHP, Python, or Ruby.
REST APIs Deletes an object using JSON and XML REST APIs.

Bulk object deletion

Bulk deletion methods are designed for high-efficiency operations on large datasets. Use bulk deletion to manage storage costs and automate data hygiene across buckets. The following are some of the common reasons for deleting multiple objects:

  • Cost optimization: Removing large datasets, such as old logs or temporary build artifacts, that are no longer needed for operations.

  • Compliance automation: Automatically enforcing data retention policies by deleting objects after a specific period, for example, 30 days.

  • Bucket migration or decommissioning: Clearing out millions or billions of objects when migrating data or closing a project.

  • Data pipeline cleanup: Removing intermediate processing files after a final report or dataset has been generated.

Tools to delete objects in bulk

To delete objects in bulk, use one of the following tools. These methods are optimized for higher throughput and reduced overhead compared to individual requests.

Tool Description
Delete multiple objects using the XML API

A programmatic method to delete up to 1,000 objects in a single POST request. The multi-object delete XML API is designed to be interoperable with the Amazon S3 API. As a result, you can use your existing Amazon S3 tools, libraries, and workflows to manage objects in Cloud Storage. The XML API is useful if you are migrating data or managing resources across both platforms.

When using the XML API to delete multiple objects, Cloud Storage generates data access audit logs. These logs include details on the overall request and individual object deletions, provided that you enable the data access audit logs. For details, see Audit logs for multi-object delete XML API.

The multi-object delete XML API has the following limitations:

  • Governance mode bypass: Cloud Storage doesn't support bypassing governance mode during any deletes, unlike Amazon S3 that allows deletion of objects that are locked by governance type by specifying the x-amz-bypass-governance-retention header. Cloud Storage enforces object retention policies to help with data immutability.
  • Delete markers: Amazon S3 delete markers aren't supported in Cloud Storage for either single or multi-object deletes. Restoring a non-current object in Cloud Storage requires using PUT requests to copy the required version to the live object.
  • Conditional deletes: The Cloud Storage multi-object delete API doesn't support conditional deletes based on ETag, Last-Modified, or Size. Requests including these parameters result in a 400 error. For conditional operations in Cloud Storage, use headers such as x-goog-if-generation-match or x-goog-if-metageneration-match.
Google Cloud console The Google Cloud console is a recommended option when deleting up to one million objects. After you initiate a deletion request, the process occurs in the background. You can check the status of your bulk deletion by clicking the Notifications button () in the Google Cloud console header.
Storage batch operations

A fully managed service that performs deletions based on a manifest file. Suitable for deleting up to billions of objects with built-in tracking and retries.

We recommend using storage batch operations if you want to do the following:

  • Delete millions or billions of objects.
  • Enhance reliability with built-in tracking, retries, and error handling.
  • Reduce client-side overhead compared to scripting individual API calls.
gcloud storage rm --recursive A Google Cloud CLI command to delete all objects sharing a common prefix. Suitable for small-to-medium scale bulk tasks using the CLI.
Object Lifecycle Management A server-side feature that deletes objects automatically based on rules you define, such as object age or storage class. To bulk delete objects in your bucket, set a lifecycle configuration rule on your bucket where the condition has Age set to 0 days and the action is set to delete. After you set the rule, Cloud Storage performs the bulk delete asynchronously.
Batch requests When using certain client libraries or when using the JSON API directly, you can batch your deletion requests to reduce the number of HTTP connections that you need.

Choose a deletion method

The tool for deleting objects depends on how many objects you need to delete and why. Use the following guidance to choose a method for your use case:

  • If you need to delete one or only a few specific objects, use the Google Cloud console, gcloud storage rm, or single API calls using client libraries or REST APIs. For details, see Delete a single object.
  • If you need to delete up to 1,000 objects in a single API request, use the multi-object delete XML API, the Amazon S3 CLI, or an S3-compatible client library like Boto3. For details, see Delete objects in bulk.
  • If you are deleting many objects that share a common name prefix, use the gcloud storage rm --recursive command. For details, see Delete objects in bulk using Google Cloud CLI.
  • If you need to delete millions or billions of objects, use Storage batch operations for reliable large-scale deletions.
  • If you need to automatically delete objects when they meet certain criteria, use Object Lifecycle Management.

What's next