10

How do I remove all traces of a Vagrant box without having to go into the file system manually?

I am creating a custom box using Packer, building and destroying a lot of test cases. When finished with a test case, I run

vagrant destroy

The vagrant help describes this command as

stops and deletes all traces of the vagrant machine

Everything seems well with the world. However, when I create a new box, after a brand new installation, I notice that my message of the day says the VM was created several days ago. It turns out boxes are (also) stored in .vagrant.d\boxes. Vagrant is using those boxes rather than, or in addition to, the boxes created by Packer, which live in a completely different location.

The vagrant documentation for vagrant destroy recommends using vagrant box remove.

Now, there's still a box in .vagrant.d\boxes directory. Using vagrant box remove requires a "name".

If I run vagrant box list, it says

There are no installed boxes! Use vagrant box add to add some.

Apparently Vagrant doesn't know about this other box. Yet if I run vagrant global-status, I get an output like

id       name    provider   state    directory
-------------------------------------------------------------------------
47d1c7c  default virtualbox poweroff C:/some/path

So, Vagrant can see some box. Trying to use vagrant box remove, neither the "name" or "id" references work:

C:\some\path>vagrant box remove default
The box you requested to be removed could not be found. No
boxes named 'default' could be found.

C:\some\path>vagrant box remove 47d1c7c
The box you requested to be removed could not be found. No
boxes named '47d1c7c' could be found.

Of course, I can just delete the boxes by hand in .vagrant.d\boxes.

It seems like my understanding of what Vagrant means by 'box', 'destroy', 'remove', and, well, almost all the terms involved here, isn't correct. I can't make sense of it all. There's a Vagrant VM directory which contains one box, but then there's the box Packer creates, and there's yet another box in .vagrant.d\boxes. To destroy a box removes some of the traces, but not all. The list command sees some boxes but not others. It all appears very inconsistent.

I'm creating CentOS 7 guests on a Windows 7 host, if that matters.

4 Answers4

8

Vagrant creates an internal Virtual Machine related to a downloaded box.

To delete all data from the Virtual Machine and Box I do recommend removing the box and also destroying the virtual machine.

To list the boxes execute the command:

vagrant box list

To remove a box execute the command:

vagrant box remove box/name

To destroy the Virtual Machine

vagrant destroy vm-name

You wiped out all data.

1

@Loren Ipsum I did a backup of my whole directory with VagrantFile, so I removed the whole directory. Doing a #vagrant global-status --prune remove all from this list. Delete .vagrant.d\boxes

C:\tools\Cmder
λ vagrant global-status --prune
id       name   provider state  directory
--------------------------------------------------------------------
There are no active Vagrant environments on this computer! Or,
you haven't destroyed and recreated Vagrant environments that were
started with an older version of Vagrant.

C:\tools\Cmder
λ vagrant box list
centos/7                (hyperv, 1905.1)
debian/buster64         (virtualbox, 10.0.0)
debian/contrib-buster64 (virtualbox, 10.1.0)
debian/jessie64         (virtualbox, 8.11.1)
ubuntu/trusty64         (virtualbox, 20190514.0.0)
ubuntu/xenial64         (virtualbox, 20200204.0.0)

Some box still show at vagrant box list, even doing the script bellow:

Vagrant box list

C:\Users\Marlon\OneDrive (master -> origin)
λ vagrant box list | cut -f 1 -d ' ' | xargs -L 1 vagrant box remove -f
You requested to remove the box 'centos/7'. This box has
multiple providers. You must explicitly select a single
provider to remove with `--provider`.

Available providers: hyperv, virtualbox
You requested to remove the box 'centos/7'. This box has
multiple providers. You must explicitly select a single
provider to remove with `--provider`.

Available providers: hyperv, virtualbox
Removing box 'debian/buster64' (v10.0.0) with provider 'virtualbox'...
Removing box 'debian/contrib-buster64' (v10.1.0) with provider 'virtualbox'...
Removing box 'debian/jessie64' (v8.11.1) with provider 'virtualbox'...
Removing box 'ubuntu/trusty64' (v20190514.0.0) with provider 'virtualbox'...
Removing box 'ubuntu/xenial64' (v20200204.0.0) with provider 'virtualbox'...

C:\Users\Marlon\OneDrive (master -> origin)
λ vagrant box list
centos/7 (hyperv, 1905.1)
centos/7 (virtualbox, 1905.1)

To be honest I don't know why some boxes still show above (same provider). I did manually:

C:\Users\Marlon\OneDrive (master -> origin)
λ vagrant box remove centos/7 --provider=virtualbox --force
Removing box 'centos/7' (v1905.1) with provider 'virtualbox'...

C:\Users\Marlon\OneDrive (master -> origin)
λ vagrant box remove centos/7 --provider=hyperv --force
Removing box 'centos/7' (v1905.1) with provider 'hyperv'...

C:\Users\Marlon\OneDrive (master -> origin)
λ vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.

I removed all boxes this way, tried several commands without using the file system but unfortunately it seems that is the way...

Marlon
  • 408
0

Based on my experience with vagrant, I recommend:

  1. Don't change parameters of VM in Vagrantfile before destroy a machine

  2. If it has happened - You need add an information (vm name) of the machine what you need to destroy and safely destroy it through CMD:

    vagrant destroy vmname|id
    

Note: you don't need to do any manipulations with a box to re-setup a VM actually.

Dominique
  • 2,373
-1

For me, on Windows 10, I just deleted the image in Virtual Box after deleting the main items mentioned above. This was about 6gb alone.