Remove ALL your local Vagrant Boxes via this bash command

Assuming you have only one box per provider, this command will delete ALL Vagrant boxes you currently have on your system:

$ vagrant box list | cut -f 1 -d ' ' | xargs -L 1 vagrant box remove -f

This command does the following:

  1. vagrant box list: Prints out a list of all installed vagrant boxes (with two columns—box name or path, and meta info)
  2. cut -f 1 -d ' ': Cuts the list and takes out just the first column (using spaces to delimit the columns)
  3. xargs -L 1 vagrant box remove -f: Use xargs to run one command per line, running the command vagrant box remove -f [box name from list/cut].

You can use xargs' -t option to output the commands being run just before they're executed. And if you have multiple boxes per provider, or if you have multiple versions of the same box, you'll likely need to modify the command a bit.

Since I maintain a half-dozen open-source Vagrant boxes, and use a few dozen other boxes frequently in testing and building software, I like being able to clean out old boxes quickly to conserve disk space (same thing with Docker—I frequently delete ALL downloaded images). This also forces me to make sure that all my infrastructure is 100% automated—if I have any special snowflakes around that I'd be afraid to delete by removing its base box... then that's a bug :)

Comments

In some cases there are several versions of the same box and the name is the same. When that happens you may want to use the --box-version parameter in your script as well: vagrant box remove centos/7 --box-version 1611.01

in my case:

You requested to remove the box 'ubuntu/xenial64' with provider
'virtualbox'. This box has multiple versions. You must
explicitly specify which version you want to remove with
the `--box-version` flag or specify the `--all` flag to remove all
versions. The available versions for this box are:

* 20180713.0.0
* 20180720.0.0
* 20180724.0.0
* 20180727.0.0
* 20180806.0.0
* 20180808.0.0
* 20180822.0.0
* 20180831.0.0
* 20180912.0.0
* 20180917.0.0
* 20181012.0.0
* 20181018.0.0

the solution is :

vagrant box list | cut -f 1 -d ' ' | xargs -L 1 vagrant box remove -f --all