8

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 Answers 4

5

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.

0
0

@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...

3
  • I recall hearing it was a Vagrant bug. You might have luck in the issue tracker: github.com/hashicorp/vagrant/issues Commented Mar 31, 2020 at 22:43
  • @LoremIpsum Sorry, tried to do the best to help you. Because I was with many problems with this provisioner. If you see my last questions about version 2.2.6 has a but too. Please point in the issues the exact number so I can reply in the right way. Unfortunatelly I'm trying to do a complete pipeline CI/CD and vagrant will be provisioner for the VMs and it seems that has more bugs than a lamp at night.
    – Marlon
    Commented Apr 1, 2020 at 23:19
  • I appreciate you contributing your observations! Commented Apr 2, 2020 at 18:11
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.

0
-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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .