rm

Remove a “folder” or a set of files matching a pattern.

Use with caution!

Note

rm is a high-level command that under the hood utilizes multiple calls to the server, which means the server cannot guarantee consistency between multiple operations. For example if a file matching a pattern is uploaded during a run of rm command, it MIGHT be deleted (as “latest”) instead of the one present when the rm run has started.

If a file is in governance retention mode, and the retention period has not expired, adding –bypass-governance is required.

To list (but not remove) files to be deleted, use --dry-run. You can also list files via ls command - the listing behaviour is exactly the same.

Progress is displayed on the console unless --no-progress is specified.

Use –threads to manually adjust the number of threads used in the operation. Otherwise, the number of threads will be automatically chosen.

The --versions option selects all versions of each file, not just the most recent.

The --recursive option will descend into folders, and will select only files, not folders.

The --with-wildcard option will allow using *, ? and `[]` characters in folderName as a greedy wildcard, single character wildcard and range of characters. It requires the --recursive option. Remember to quote folderName to avoid shell expansion.

The –include and –exclude flags can be used to filter the files returned from the server using wildcards. You can specify multiple –include and –exclude filters. The order of filters matters. The last matching filter decides whether a file is included or excluded. If the given list of filters contains only INCLUDE filters, then it is assumed that all files are excluded by default.

The --dry-run option prints all the files that would be affected by the command, but removes nothing.

Normally, when an error happens during file removal, log is printed and the command goes further. If any error should be immediately breaking the command, --fail-fast can be passed to ensure that first error will stop the execution. This could be useful to e.g. check whether provided credentials have deleteFiles capabilities.

Note

Using --fail-fast doesn’t prevent the command from trying to remove further files. It just stops the progress. Since multiple files are removed in parallel, it’s possible that just some of them were not reported.

Command returns 0 if all files were removed successfully and a value different from 0 if any file was left.

Examples.

Note

Note the use of quotes, to ensure that special characters are not expanded by the shell.

Note

Use with caution. Running examples presented below can cause data-loss.

Remove all csv and tsv files (in any directory, in the whole bucket):

b2 rm --recursive --with-wildcard "b2://bucketName/*.[ct]sv"

Remove all info.txt files from buckets bX, where X is any character:

b2 rm --recursive --with-wildcard "b2://bucketName/b?/info.txt"

Remove all pdf files from buckets b0 to b9 (including sub-directories):

b2 rm --recursive --with-wildcard "b2://bucketName/b[0-9]/*.pdf"

Requires capability:

  • listFiles

  • deleteFiles

  • bypassGovernance (if –bypass-governance is used)

b2 rm [-h] [--bypass-governance] [--dry-run] [--queue-size QUEUE_SIZE]
      [--no-progress] [--fail-fast] [--threads THREADS] [--versions] [-r]
      [--with-wildcard] [--include FILTERS] [--exclude FILTERS]
      B2_URI

Positional Arguments

B2_URI

B2 URI pointing to a bucket, directory or a file. e.g. b2://yourBucket, b2://yourBucket/file.txt, b2://yourBucket/folderName/, or b2id://fileId

Named Arguments

--bypass-governance

Default: False

--dry-run

Default: False

--queue-size

max elements fetched at once for removal, if left unset defaults to twice the number of threads.

--no-progress

Default: False

--fail-fast

Default: False

--threads
--versions

Default: False

-r, --recursive

Default: False

--with-wildcard

Default: False

--include

Default: []

--exclude

Default: []