Shell Functions to Recursively Delete/Suspend all Vagrant Instances
I have been using Vagrant exclusively for almost a year now and am still loving it, even with the introduction of Docker. One issue I do find that arises is managing currently running and obsolete instances. Port conflicts can be a huge pain to correct if you are like me and use Vagrant for many different projects at one time, resorting to loading up VirtualBox. To help ease the pain I have created two shell script functions (tested in Bash) for recursively locating and either deleting or suspending Vagrant instances it finds.
function vagrant-destroy-all() {
DIR=${1:-$(pwd)}
find $DIR \
-type d \
-name .vagrant \
-exec sh -c '(cd $(dirname {}) && pwd && vagrant destroy -f)' \;
}
function vagrant-suspend-all() {
DIR=${1:-$(pwd)}
find $DIR \
-type d \
-name .vagrant \
-exec sh -c '(cd $(dirname {}) && pwd && vagrant suspend)' \;
}
The two functions above allow you to provide a starting directory to locate instances, based on the existence of a ‘.vagrant’ directory. If no path is provided the present working directory is used instead. Finally progress of each found instance and actions taken is printed to the terminal.