This page shows you how to delete objects from your buckets in Cloud Storage. For an overview of object deletion methods, see About object deletion.
Required roles
To get the permissions that you need to delete objects, ask your administrator to grant you the following IAM roles on the bucket that contains the objects you want to delete:
-
Delete objects using Google Cloud CLI or REST APIs:
Storage Object User (
roles/storage.objectUser) -
Delete objects using the Google Cloud console:
Storage Admin (
roles/storage.admin) -
Alternatively, to delete objects using the Google Cloud console:
Viewer (
roles/viewer) and Storage Object User (roles/storage.objectUser)
For more information about granting roles, see Manage access to projects, folders, and organizations.
These predefined roles contain the permissions required to delete objects. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to delete objects:
-
Delete objects:
storage.objects.delete -
List objects using the Google Cloud console, or using the
--recursiveflag or wildcards in Google Cloud CLI:storage.objects.list -
List buckets using the Google Cloud console:
storage.buckets.list
You might also be able to get these permissions with custom roles or other predefined roles.
Delete a single object
This section shows how to delete one object at a time.
Complete the following steps to delete a single object:
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
In the list of buckets, click the name of the bucket that contains the objects you want to delete.
The Bucket details page opens, with the Objects tab selected.
Navigate to the object, which may be located in a folder.
Select the checkbox for the object you want to delete.
Click Delete, and then click Delete in the dialog that appears.
Command line
Use the Google Cloud CLI command gcloud storage rm:
gcloud storage rm gs://BUCKET_NAME/OBJECT_NAME
Where:
BUCKET_NAMEis the name of the bucket containing the object you want to delete. For example,my-bucket.OBJECT_NAMEis the name of the object you want to delete. For example,pets/dog.png.
If successful, the response is similar to the following example:
Removing objects: Removing gs://example-bucket/file.txt... Completed 1/1
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
Rust
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorizationheader.Use
curlto call the JSON API with aDELETErequest:curl -X DELETE \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME"
Where:
BUCKET_NAMEis the name of the bucket containing the object you want to delete. For example,my-bucket.OBJECT_NAMEis the URL-encoded name of the object you want to delete. For example,pets/dog.png, URL-encoded aspets%2Fdog.png.
XML API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorizationheader.Use
curlto call the XML API with aDELETE Objectrequest:curl -X DELETE \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME"
Where:
BUCKET_NAMEis the name of the bucket containing the object you want to delete. For example,my-bucket.OBJECT_NAMEis the URL-encoded name of the object you want to delete. For example,pets/dog.png, URL-encoded aspets%2Fdog.png.
Delete objects in bulk
This section shows how to delete objects in bulk by selecting them in the Google Cloud console, by deleting objects with a common prefix using command-line tools, or by specifying a list of objects in API or client library requests.
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
In the list of buckets, click the name of the bucket that contains the objects you want to delete.
The Bucket details page opens, with the Objects tab selected.
Navigate to the objects, which may be located in a folder.
Select the checkbox for each object you want to delete.
You can select the checkbox for a folder, which deletes all objects contained in that folder.
Click Delete, and then click Delete in the dialog that appears.
If you delete many objects at once, you can track deletion progress by clicking the Notifications icon in the Google Cloud console. The Google Cloud console can bulk delete up to several million objects and does so in the background.
To learn how to get detailed error information about failed Cloud Storage operations in the Google Cloud console, see Troubleshooting.
Command line
Google Cloud CLI
To delete groups of objects that have the same prefix, such as
objects whose names mimic a folder structure, use the
--recursive flag with gcloud storage rm:
gcloud storage rm --recursive gs://BUCKET_NAME/PREFIX
Where:
BUCKET_NAMEis the name of the bucket. For example,my-bucket.PREFIXis common prefix of the objects you want to delete. For example,pets/.
Amazon S3 CLI
To delete multiple objects in Cloud Storage using the Amazon S3
CLI, use the aws s3api delete-objects command. You'll need to redirect the
request to the Cloud Storage multi-object delete XML API by
setting the --endpoint-url flag to storage.googleapis.com. For detailed parameter
definitions and behavior of delete-objects API, see the Amazon S3 CLI
delete-objects reference documentation.
Client libraries
Cloud Storage supports the multi-object delete XML API through its Amazon S3-compatible interface.
To use Amazon S3-compatible client libraries for bulk object deletion, point
your request to the Google Cloud endpoint by setting the endpoint URL to https://storage.googleapis.com in the client configuration. This approach can be beneficial if you are performing any of the following tasks:
- Leveraging existing codebases or tooling already built for Amazon S3.
- Maintaining consistency across multi-cloud environments that include both Amazon S3 and Cloud Storage.
The following example shows how to initialize a client using a Boto3 library to interact with Cloud Storage:
import boto3
def main():
# Initialize the S3 client to point to the Google Cloud Storage endpoint
client = boto3.client(
service_name='s3',
endpoint_url='https://storage.googleapis.com',
aws_access_key_id='YOUR_ACCESS_ID',
aws_secret_access_key='YOUR_SECRET',
)
# Perform delete operations as defined in the library's documentation
# response = client.delete_objects(Bucket='BUCKET_NAME', Delete={'Objects': [...]})
For specific method signatures and parameter definitions, see the delete_objects Boto3 documentation.
XML API
To delete up to 1,000 objects in a single request using the XML API, complete the following steps:
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorizationheader.Create an XML document that specifies the objects to be deleted.
For a complete list of settings, see Delete multiple objects in the XML API reference documentation. The common settings to include in your XML file are as follows:
<Delete> <Object> <Key>OBJECT_NAME</Key> <VersionId>VERSION_ID</VersionId> </Object> ... <Quiet>QUIET_RESPONSE_BOOLEAN</Quiet> </Delete>Where:
OBJECT_NAMEis the URL-encoded name of the object. For example,pets/dog.png, URL-encoded aspets%2Fdog.png. You can specify up to 1,000 objects in the XML file.VERSION_IDis the version ID for the object. For example,11111.QUIET_RESPONSE_BOOLEANcontrols the verbosity of the API's response:- Set to
Falsefor a verbose response. The response includes details for every object, whether it is successfully deleted or not. The multi-object delete operation using the XML API is not atomic. If a request results in partial failures, some objects might be successfully deleted but other objects might fail to be deleted, so we recommend using the verbose response to identify all object deletions. If you have Data Access audit logs enabled, you can also review audit logs for failure details. - Set to
Truefor a quiet response. The response body doesn't include information about the objects that are successfully deleted.
- Set to
Use
curlto send aPOSTbucket request to the XML API with the?deletequery parameter and your XML file:curl -X POST --data-binary @XML_FILE_NAME \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/BUCKET_NAME?delete"
Where:
XML_FILE_NAMEis the name of the XML file you created.BUCKET_NAMEis the name of the bucket containing the objects to delete.If
Quietmode is disabled, a verbose response might look similar to the following:<DeleteResult> <Deleted> <Key>images/photo1.jpg</Key> <VersionId>11111</VersionId> </Deleted> <Deleted> <Key>documents/report.pdf</Key> <VersionId>22222</VersionId> </Deleted> <Error> <Code>AccessDenied</Code> <Key>private/financial_data.xlsx</Key> <Message>Access Denied. You don't have permission to delete this object.</Message> <VersionId>33333</VersionId> </Error> </DeleteResult>
If
Quietmode is enabled, the response lists only objects that failed to delete. If all objects are successfully deleted, an empty<DeleteResult/>tag is returned. The following is an example of a quiet response where an error occurred:<DeleteResult> <Error> <Code>AccessDenied</Code> <Key>private/financial_data.xlsx</Key> <Message>Access Denied. You don't have permission to delete this object.</Message> <VersionId>33333</VersionId> </Error> </DeleteResult>
Delete up to billions of objects
To delete millions or billions of objects with a single object deletion job, use storage batch operations. To create a job, specify the objects to delete, either by providing a list of objects in a manifest file or by using object prefixes. After you have specified the object list, create a batch operation job to delete the objects.
Automatically delete objects using object lifecycle rules
If you want objects to be deleted automatically when they meet criteria that you specify, such as age or storage class, use Object Lifecycle Management. For example, you can set a lifecycle rule to delete logs that are older than 30 days.
Batch object deletion requests with JSON API
To reduce the number of HTTP connections needed when deleting many objects with the JSON API, use JSON API batch requests. You can group up to 100 API calls into a single HTTP request to help reduce network overhead.
What's next
- Learn how to restore soft-deleted objects if you deleted an object accidentally.
- If you use Object Versioning, understand that deleting an object might make it noncurrent rather than deleting it permanently. Learn how to manage or permanently delete noncurrent versions.
- Configure Object Lifecycle Management to automate future object deletions.
- Delete a bucket if you no longer need it.