Skip to content

Instantly share code, notes, and snippets.

@kjkent
Forked from Maxzor/remove-all-from-docker-oneliner.sh
Last active October 11, 2022 10:34
Show Gist options
  • Save kjkent/aca23152d44a70f9e4749e4643f1acdb to your computer and use it in GitHub Desktop.
Save kjkent/aca23152d44a70f9e4749e4643f1acdb to your computer and use it in GitHub Desktop.
Docker "rm everything"
#!/bin/bash
cls() { docker container ls -qa; }
ils() { docker image ls -qa; }
vls() { docker volume ls -q; }
nls() {
an=$(docker network ls | awk '(NR > 1) {print $1" "$2}')
# remove host default networks
echo $an | grep -v 'bridge\|host\|none' | awk '{print $1}'
}
containers=$(cls)
if [ -n "$containers" ]; then
echo "Containers: $containers"
echo "Stopping"
docker stop $containers
echo "Removing"
docker rm $containers
unresponsive=$(cls)
if [ -n "$unresponsive" ]; then
echo "Unresponsive; killing: $unresponsive"
docker rm -f $unresponsive
fi
fi
images=$(ils)
if [ -n "$images" ]; then
echo "Removing images: $images"
docker rmi -f $images
fi
volumes=$(vls)
if [ -n "$volumes" ]; then
echo "Removing volumes: $volumes"
docker volume rm -f $volumes
fi
networks=$(nls)
if [ -n "$networks" ]; then
echo "Removing networks: $networks"
docker network rm $networks;
fi
c=$(cls); i=$(ils); v=$(vls); n=$(nls)
if [ -n "$c$i$v$n" ]; then
echo "Unable to be removed:"
if [ -n "$c" ]; then echo "Containers: $c"; fi
if [ -n "$i" ]; then echo "Images: $i"; fi
if [ -n "$v" ]; then echo "Volumes: $v"; fi
if [ -n "$n" ]; then echo "Networks: $n"; fi
exit 1;
else
echo "Success"; exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment