☰

Adding and removing files in bucket using cloud SDK.

Adding and Removing Files in a Bucket With gsutil

gsutil is the command line tool for Cloud Storage, included with the Google Cloud SDK. It covers the same everyday operations as the console, uploading, downloading, and copying files, but as a command you can run repeatedly, put in a script, or run from a machine that never opens a browser at all.

This walks through copying a file from your local machine into a bucket, pulling one back down, copying directly between two buckets, and then the part this guide is actually named for, removing a file for good.

Step by Step Process of Adding and Removing Files

Step One: Copy a File From Your Local Machine to a Bucket

gsutil cp LOCAL_FILE_PATH gs://BUCKET_NAME/

Step Two: Copy a File From a Bucket to Your Local Machine

gsutil cp gs://BUCKET_NAME/FILE_NAME LOCAL_DESTINATION_PATH

Using cloud Shell

Step Three: Copy a File From One Bucket to Another

gsutil cp gs://SOURCE_BUCKET_NAME/FILE_NAME gs://DESTINATION_BUCKET_NAME/FILE_NAME

Using cloud shell

Removing a File From a Bucket

To delete a single object from a bucket:

gsutil rm gs://BUCKET_NAME/FILE_NAME

This deletes the object outright. gsutil itself gives no confirmation prompt before removing it, so double check the path before running this, especially with a wildcard.

Removing Many Files at Once

To remove everything inside a folder like path without deleting the bucket itself:

gsutil rm gs://BUCKET_NAME/FOLDER_NAME/**

For a large number of objects, add the multi threading flag to speed the operation up:

gsutil -m rm gs://BUCKET_NAME/FOLDER_NAME/**

Adding -r instead recursively removes a folder like path and everything under it, and on a versioning enabled bucket, -r also removes every stored version of each object, not just the current one.

Can a Deleted File Be Recovered

By default, Cloud Storage now keeps a deleted object in a recoverable, soft deleted state for a set number of days before it is gone for good, which can be restored with gcloud storage restore if you catch the mistake in time. This is a genuine safety net that did not always exist, but it is not unlimited, and it can be disabled on a bucket. If your bucket has soft delete turned off, gsutil rm is immediate and final, so treat it that way regardless.

Common Mistakes to Avoid

  • Running rm with a wildcard without checking the path first. List the objects you are about to remove before deleting them if there is any doubt.
  • Assuming rm always has a safety net. Soft delete helps if it is enabled on the bucket, but it is not guaranteed to be on, and it does not last forever.
  • Confusing -r with removing only the live version of an object. On a versioning enabled bucket, -r removes every stored version, not just the current one.
  • Copying a command with a broken or mismatched placeholder. Make sure both sides of a cp command use the same bracket style so it is obvious what to actually replace.

That covers copying files in and out of a bucket with gsutil, and the delete command this page was always meant to include. To go further, explore Prwatech’s Google Cloud training program, which includes placement assistance.

Popular Tags:

adding files Cloud SDK GCP gcp certification gcp cloud console gcp storage google cloud certification google cloud console Google Cloud Platform google cloud services removing files