You might have downloaded several versions of Vagrant boxes and some of them might be pretty outdated! If they are no longer required, you can safely delete outdated Vagrant boxes in Linux as described in this brief guide.
Check for outdated Vagrant boxes
I have been using Vagrant for the past few months for testing purposes. Since Vagrant version 1.5, boxes support versioning. The Box Versioning allows the developers who make boxes to push updates or fixes and the users to easily update the underlying box.
If a box is out of date, the user will be notified when he starts the vagrant environment using vagrant up
command:
$ vagrant up
Sample output:
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'generic/alpine38' version '3.1.16' is up to date...
==> default: A newer version of the box 'generic/alpine38' for provider 'virtualbox' is
==> default: available! You currently have version '3.1.16'. The latest is version
==> default: '3.1.22'. Run `vagrant box update` to update.
==> default: Clearing any previously set forwarded ports...
...
The user can also manually check for the outdated boxes in your current Vagrant environment using vagrant box outdated
command:
$ vagrant box outdated
This command will show you the list of boxes that need to be updated.
Checking if box 'generic/alpine38' version '3.1.16' is up to date...
A newer version of the box 'generic/alpine38' for provider 'virtualbox' is
available! You currently have version '3.1.16'. The latest is version
'3.1.22'. Run `vagrant box update` to update.
If you want to check for all installed boxes, simply add --global
flag at the end:
$ vagrant box outdated --global
Sample output:
/usr/share/rubygems-integration/all/gems/vagrant-2.2.6/plugins/commands/box/command/outdated.rb:65: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call /usr/share/rubygems-integration/all/gems/vagrant-2.2.6/lib/vagrant/box.rb:124: warning: The called method `load_metadata' is defined here * 'oraclelinux/8' for 'libvirt' (v8.3.183) is up to date * 'oraclelinux/7' for 'virtualbox' is outdated! Current: 7.9.184. Latest: 7.9.185 * 'generic/alpine38' for 'virtualbox' is outdated! Current: 3.1.16. Latest: 3.1.22 * 'fedora33' for 'virtualbox' wasn't added from a catalog, no version information * 'archlinux/archlinux' for 'virtualbox' is outdated! Current: 20201215.11392. Latest: 20210115.13749 * 'archlinux/archlinux' for 'virtualbox' is outdated! Current: 20201201.10292. Latest: 20210115.13749 * 'Fedora33' for 'libvirt' wasn't added from a catalog, no version information
As you can see in the above output, I have a some outdated boxes.
The users can update the box associated with current Vagrant environment with command:
$ vagrant box update
The above command downloads the new version of box and installs it. All downloaded boxes are saved in ~/.vagrant.d/boxes
directory in your host system. The older version of the box will remain available in the same location until you manually remove that Vagrant box from the cache folder.
You can verify if by listing the all installed Vagrant boxes with command:
$ vagrant box list
Sample output:
Fedora33 (libvirt, 0)
archlinux/archlinux (virtualbox, 20201201.10292)
archlinux/archlinux (virtualbox, 20201215.11392)
fedora33 (virtualbox, 0)
generic/alpine38 (virtualbox, 3.1.16)
oraclelinux/7 (virtualbox, 7.9.184)
oraclelinux/8 (libvirt, 8.3.183)
As you can see in the above output, I have two versions of Arch Linux vagrant boxes. There is no need to keep two boxes of same OS. So let us remove the outdated vagrant box.
Delete Outdated Vagrant Boxes
You can use vagrant box prune
command to remove the outdated Vagrant boxes in your Linux system.
First, let us verify which boxes will be deleted and which will be kept with --dry-run
option. The option is used to simulate commands without changing anything in a Linux system.
$ vagrant box prune --dry-run
This command will not actually delete any boxes, but only print the boxes that would be removed.
Sample output:
The following boxes will be kept...
Fedora33 (libvirt, 0)
archlinux/archlinux (virtualbox, 20201215.11392)
fedora33 (virtualbox, 0)
generic/alpine38 (virtualbox, 3.1.16)
oraclelinux/7 (virtualbox, 7.9.184)
oraclelinux/8 (libvirt, 8.3.183)
Checking for older boxes...
Would remove archlinux/archlinux virtualbox 20201201.10292
As you see in the above output, the vagrant box named "archlinux/archlinux virtualbox 20201201.10292
" will be removed from your system.
You know now which box is going to be removed. If it is OK for you, simply run the same command command without --dry-run
option:
$ vagrant box prune
This command will keep the current updated boxes and remove all other outdated boxes.
The following boxes will be kept...
Fedora33 (libvirt, 0)
archlinux/archlinux (virtualbox, 20201215.11392)
fedora33 (virtualbox, 0)
generic/alpine38 (virtualbox, 3.1.16)
oraclelinux/7 (virtualbox, 7.9.184)
oraclelinux/8 (libvirt, 8.3.183)
Checking for older boxes...
Removing box 'archlinux/archlinux' (v20201201.10292) with provider 'virtualbox'...
Vagrant-libvirt plugin removed box only from you LOCAL ~/.vagrant/boxes directory
From libvirt storage pool you have to delete image manually(virsh, virt-manager or by any other tool)
Now verify the list of installed boxes:
$ vagrant box list Fedora33 (libvirt, 0) archlinux/archlinux (virtualbox, 20201215.11392) fedora33 (virtualbox, 0) generic/alpine38 (virtualbox, 3.1.16) oraclelinux/7 (virtualbox, 7.9.184) oraclelinux/8 (libvirt, 8.3.183)
See? Now it shows only one archlinux vagrant box. The outdated box is deleted.
The prune
command has other useful options as well. You can use -p
, --provider
option to destroy boxes with specific PROVIDER (E.g. Virtualbox or libvirt).
The -f
, --force
flag destroys without confirmation even when the box is in use. The -k
, --keep-active-boxes
option is used to keep boxes still actively in use.
To display help section, run:
$ vagrant box prune --help
1 comment
Hi,
Thank you so much for the great topic.