How Can I Easily Delete All Files in a Bucket? How Can I Easily Delete All Files in a Bucket?

How Can I Easily Delete All Files in a Bucket?

Anthony Anthony

There are three ways that you can delete all Files in a Bucket, the First is through the Command Line Tool, the Second is through the Web UI, and finally by using Lifecycle Rules.


Command Line Tool:


To easily delete all files in a bucket, first, install the B2 Command Line tool installed on the computer. If assistance is needed with setting up this installation, please refer to our Command Line tool documentation here. This guide assumes you are using the Python version of the CLI.


Once the Command Line tool is installed, follow these steps:

1) Open Terminal.
2) Create a new local directory (e.g., mkdir empty)
3) Change into that directory (e.g., cd empty)
4) Run the command: b2 authorize-account [<KeyID>] [<ApplicationKey>]
5) Run the following command: b2 sync --allowEmptySource --delete . b2://mybucketname where mybucketname is the name of the bucket you wish to empty out.

This will sync the empty contents of the local directory to the bucket, deleting all files.  


However, if there are still unfinished large files in the bucket, this method by itself will not remove those unfinished listings. The bucket may still report as not being fully empty.

In order to remove unfinished large files, call

b2 cancel-all-unfinished-large-files <bucketName>

Then, try to delete the bucket once more.


Web UI:


1. Log in to your account and go to the page named "browse files" under the "B2 Cloud Storage" heading.


2. Once there you will want to select which bucket you wish to delete files from the list of buckets




3. Once the bucket is selected, click the checkmark at the top of the file list next to "Name" to select all files in the bucket, and then click on the button labeled "delete"



4. You will then be presented with a dialogue to confirm the deletion of the files.




Please note that if you have a very large number of files to delete that the CLI method is better equipped to handle large deletions.



Lifecycle Rules:


1. To delete all files using Lifecycle Rules set both values to 1

For example:
daysFromUploadingToHiding → 1

daysFromHidingToDeleting → 1




2. If you want to limit the files to a certain prefix, enter that into the File Path section. Otherwise, leave the value for "File Path" and this rule will apply to all files within the bucket.


3. Doing the above will have all designated files be hidden after 24-48 hours, and deleted 24 hours after that. Using this process, all files will be erased within 72 hours.


Deleting a Bucket:


1.  After all Files are deleted, you can fully delete the bucket by going to the "Buckets" page of your account. Once there select "Bucket Settings".




2. Clicking "Bucket Settings" will give you a pop-up like shown below. Select the red button labeled "Delete Bucket" to fully remove the bucket from your account