-
-
Save beeman/aca41f3ebd2bf5efbd9d7fef09eac54d to your computer and use it in GitHub Desktop.
# Stop all containers | |
docker stop `docker ps -qa` | |
# Remove all containers | |
docker rm `docker ps -qa` | |
# Remove all images | |
docker rmi -f `docker images -qa ` | |
# Remove all volumes | |
docker volume rm $(docker volume ls -qf) | |
# Remove all networks | |
docker network rm `docker network ls -q` | |
# 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 | |
nice
the "f" parameter in docker volume rm $(docker volume ls -qf)
is for filter. if you don't specify it, it'll raise an error. But it's safe to ommit the f parameter, or here is the list of available filters:
- dangling (boolean - true or false, 0 or 1)
- driver (a volume driver’s name)
- label (label= or label==)
- name (a volume’s name)
Example:
docker volume rm $(docker volume ls -qf dangling="true")
@samhk222 docker volume rm $(docker volume ls -qf dangling="true")
works perfectly! Thanks a lot!
Oneliner
docker stop `docker ps -qa` && docker rm `docker ps -qa` && docker rmi -f `docker images -qa ` && docker volume rm $(docker volume ls -qf) && docker network rm `docker network ls -q`
Better and more destructive oneliner
docker stop $(docker ps -qa); docker rm $(docker ps -qa); docker rmi -f $(docker images -qa); docker volume rm $(docker volume ls -q); docker network rm $(docker network ls -q)
<3 you save me. Thank you
thnx !! 😃
Shell scripts are damn ugly. But shell is so powerful 🙃
Why is rm -rf --all
not in the docker binary lol...
Sigh, another try which does the same, but outputs less noise and adds a bit of sugar :
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;
Readable bash script at https://gist.github.com/Maxzor/6a3ca2c5c1c28af583711abc8e5fda01
Pre-defined network(bridge
host
none
) can be ignored.
docker network rm `docker network ls -q --filter type=custom`
This will get rid of the following error message:
Error response from daemon: bridge is a pre-defined network and cannot be removed
Error response from daemon: host is a pre-defined network and cannot be removed
Error response from daemon: none is a pre-defined network and cannot be removed
Oneliner
docker stop `docker ps -qa` && docker rm `docker ps -qa` && docker rmi -f `docker images -qa ` && docker volume rm $(docker volume ls -qf) && docker network rm `docker network ls -q`
In this case I'd personally replace the '&&' with a ';' so they all run and in series or even a single '&' to run parallel. You don't want to to stop deleting the rest if one list returns nothing. Example, say there are no volumes, this will fail at that step and not delete the networks. && is mostly useful if the next command is predicated on the previous command having completed successful.
Fastest Method:
docker stop `docker ps -qa` ; docker rm `docker ps -qa` & docker rmi -f `docker images -qa ` & docker volume rm $(docker volume ls -qf) & docker network rm `docker network ls -q`
Oneliner
docker stop `docker ps -qa` && docker rm `docker ps -qa` && docker rmi -f `docker images -qa ` && docker volume rm $(docker volume ls -qf) && docker network rm `docker network ls -q`
In this case I'd personally replace the '&&' with a ';' so they all run and in series or even a single '&' to run parallel. You don't want to to stop deleting the rest if one list returns nothing. Example, say there are no volumes, this will fail at that step and not delete the networks. && is mostly useful if the next command is predicated on the previous command having completed successful.
Fastest Method:
docker stop `docker ps -qa` ; docker rm `docker ps -qa` & docker rmi -f `docker images -qa ` & docker volume rm $(docker volume ls -qf) & docker network rm `docker network ls -q`
Somebody already did it
https://gist.github.com/beeman/aca41f3ebd2bf5efbd9d7fef09eac54d#gistcomment-3362258
Try this 👍
docker stop `docker ps -qa`
docker system prune --volumes --all
You have a typo, it should be:
docker system prune --volumes --all
(note plural volumes
)
Try this 👍
docker stop `docker ps -qa` docker system prune --volume --all
Thanks 😅
I just type without testing in a terminal.
You have a typo, it should be:
docker system prune --volumes --all
(note pluralvolumes
)Try this +1
docker stop `docker ps -qa` docker system prune --volume --all
BTW, it appears for me that the command above to delete Docker volumes is outdated; I get an error:
docker volume ls -qf
flag needs an argument: 'f' in -f
See 'docker volume ls --help'.
The command is now docker volume ls -q
, I think. Here is my docker version:
docker --version
Docker version 20.10.2, build 2291f61
I recommend:
docker stop $(docker ps -qa); docker rm $(docker ps -qa); docker rmi -f $(docker images -qa); docker volume rm $(docker volume ls -q); docker network rm $(docker network ls -q)
Here's a script that doesn't generate any errors, including if there are no containers/images/volumes/networks:
#!/bin/bash
echo "Listing containers..."
containers=$(docker ps -qa)
echo "containers: $containers"
if [ ! -z "$containers" ]
then
echo "Stopping containers..."
docker stop $containers
echo "Removing containers..."
docker rm $containers
else
echo "No containers found"
fi
echo "Listing images..."
images=$(docker images -qa)
echo "images: $images"
if [ ! -z "$images" ]
then
echo "Removing images..."
docker rmi -f $images
else
echo "No images found"
fi
echo "Listing volumes..."
volumes=$(docker volume ls -q)
echo "volumes: $volumes"
if [ ! -z "$volumes" ]
then
echo "Removing volumes..."
docker volume rm $volumes
else
echo "No volumes found"
fi
echo "Listing networks..."
networks=$(docker network ls -q)
echo "networks: $networks"
if [ ! -z "$networks" ]
then
echo "Removing networks..."
docker network rm $networks
else
echo "No networks found"
fi
echo "These should not output any items:"
docker ps -a
docker images -a
docker volume ls
echo "This should only show the default networks:"
docker network ls
docker stop $(docker ps -qa)
docker system prune -a
that's all you need to remove everything
Hey this line $(...) doesn't work with fish. Is there an alternative that works everywhere?
Just drop the $, at least for that bit of fish. Or just run bash and then paste in the command.
Here's the fish
version for clarity.
docker stop (docker ps -qa);
docker rm (docker ps -qa);
docker rmi -f (docker images -qa);
docker volume rm (docker volume ls -q);
docker network rm (docker network ls -q);
Hi, I tried docker volume rm $(docker volume ls -qf)
to remove volumes but got the following message returned:
`flag needs an argument: 'f' in -f
See 'docker volume ls --help'.
"docker volume rm" requires at least 1 argument.
See 'docker volume rm --help'.
Usage: docker volume rm [OPTIONS] VOLUME [VOLUME...]
Remove one or more volumes`
I tried this instead and successfully removed all volumes:
docker volume rm $(docker volume ls -q)
After these commands I still had volumes under docker volume ls
had to run docker volume prune
to remove them. Reclaimed almost half a gig!
thanks
You have a typo, it should be:
docker system prune --volumes --all
(note pluralvolumes
)Try this 👍
docker stop `docker ps -qa` docker system prune --volume --all
This is actually plural now --volumes
thanks!!!
Thanks
<3
This is missing:
docker builder prune
** The backtick syntax should be replaced with $()
this is awesome lol