Skip to content

Instantly share code, notes, and snippets.

@konstruktoid
Last active April 19, 2024 10:25
Show Gist options
  • Save konstruktoid/b3ff979917c54db78d6f6397ca68f892 to your computer and use it in GitHub Desktop.
Save konstruktoid/b3ff979917c54db78d6f6397ca68f892 to your computer and use it in GitHub Desktop.
Clean VirtualBox VMs, Vagrant boxes and Containers
#!/bin/bash
set -uo pipefail
CONTAINER_RUNTIME="unset"
if command -v docker >/dev/null 2>&1; then
CONTAINER_RUNTIME=docker
else
CONTAINER_RUNTIME=podman
fi
running_vms=$(VBoxManage list runningvms | awk -F\" '{print $2}')
for running in ${running_vms}; do
VBoxManage controlvm "${running}" poweroff
done
VBoxManage list vms | awk '{print $2}' | tr -d '"{}' | while read -r VM; do VBoxManage unregistervm "${VM}" --delete; done
vagrant box list | awk '{print $1}' | while read -r BOX; do vagrant box remove "${BOX}" --all; done
"${CONTAINER_RUNTIME}" rm -f "$("${CONTAINER_RUNTIME}" ps -qa)"
"${CONTAINER_RUNTIME}" rm -f "$("${CONTAINER_RUNTIME}" images -qa)"
if [ "${CONTAINER_RUNTIME}" == "docker" ]; then
docker system prune --all --force
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment