Skip to content

Instantly share code, notes, and snippets.

@Maxzor
Forked from beeman/remove-all-from-docker.sh
Last active August 13, 2024 13:12
Show Gist options
  • Save Maxzor/6a3ca2c5c1c28af583711abc8e5fda01 to your computer and use it in GitHub Desktop.
Save Maxzor/6a3ca2c5c1c28af583711abc8e5fda01 to your computer and use it in GitHub Desktop.
Remove all from Docker
echo "Removing containers :" && if [ -n "$(docker container ls -aq)" ]; then docker container stop $(docker container ls -aq); docker container rm $(docker container ls -aq); fi; echo "Removing images :" && if [ -n "$(docker images -aq)" ]; then docker rmi -f $(docker images -aq); fi; echo "Removing volumes :" && if [ -n "$(docker volume ls -q)" ]; then docker volume rm $(docker volume ls -q); fi; echo "Removing networks :" && if [ -n "$(docker network ls | awk '{print $1" "$2}' | grep -v 'ID\|bridge\|host\|none' | awk '{print $1}')" ]; then docker network rm $(docker network ls | awk '{print $1" "$2}' | grep -v 'ID\|bridge\|host\|none' | awk '{print $1}'); fi;
#!/bin/bash
# Stop and remove all containers
echo "Removing containers :"
if [ -n "$(docker container ls -aq)" ]; then
docker container stop $(docker container ls -aq);
docker container rm $(docker container ls -aq);
fi;
# Remove all images
echo "Removing images :"
if [ -n "$(docker images -aq)" ]; then
docker rmi -f $(docker images -aq);
fi;
# Remove all volumes
echo "Removing volumes :"
if [ -n "$(docker volume ls -q)" ]; then
docker volume rm $(docker volume ls -q);
fi;
# Remove all networks
echo "Removing networks :"
# Skip default networks : bridge, host, none
if [ -n "$(docker network ls | awk '{print $1" "$2}' | grep -v 'ID\|bridge\|host\|none' | awk '{print $1}')" ]; then
docker network rm $(docker network ls | awk '{print $1" "$2}' | grep -v 'ID\|bridge\|host\|none' | awk '{print $1}');
fi;
# Your installation should now be all fresh and clean.
# The following commands should not output any items:
# docker ps -a
# docker images -a
# docker volume ls
# The following command show only show the default networks:
# docker network ls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment