Skip to content

Instantly share code, notes, and snippets.

@greyltc
Last active December 7, 2022 12:16
Show Gist options
  • Save greyltc/361d6abb69d50090e62e to your computer and use it in GitHub Desktop.
Save greyltc/361d6abb69d50090e62e to your computer and use it in GitHub Desktop.
removes all traces of all docker containers/images from your system
#!/usr/bin/env bash
# this removes every single docker container, image and volume on your system
# bash <(curl --silent --location https://gist.github.com/greyltc/361d6abb69d50090e62e/raw)
#FORCE="--force"
echo "Cleaning up all the docker things!"
echo "Containers..."
for c in $(docker container ls --all --quiet)
do
docker container stop $c || docker container kill $c || echo "${c} was not running"
#docker container rm ${FORCE} --volumes $c
done
echo "Images..."
for i in $(docker image ls --all --quiet)
do
docker image rm ${FORCE} $i
done
echo "Images by repo:tag..."
for i in $(docker image ls --format "{{.Repository}}:{{.Tag}}")
do
docker image rm ${FORCE} $i
done
echo "Volumes..."
for v in $(docker volume ls --quiet)
do
docker volume rm ${FORCE} $v
done
# TODO: docker seems to be sloppy in cleaning up BTRFS subvolumes in /var/lib/docker/btrfs/subvolumes
# and it seems to "forget" about them, must figure out the safest way to clean these up too...
# here's one way to blow away docker's btrfs subvolumes:
# sudo btrfs subvolume list / | grep 'var/lib/docker/btrfs/subvolumes' | awk -F' path ' '{ print "/" $2 }' | xargs sudo btrfs subvolume delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment