Remove all Docker containers, images, networks and volumes
The modern way to clean up Docker resources is the docker system prune command. However, this guide
also covers manual cleanup methods and provides a convenient Bash function for comprehensive Docker maintenance.
Table of Contents
Modern Approach: docker system prune
Docker provides a built-in command for cleaning up unused resources. The docker system prune
command removes all stopped containers, unused networks, dangling images, and build cache:
docker system pruneTo also remove all unused images and volumes, add the -a and --volumes flags:
docker system prune -a --volumesTo skip the confirmation prompt, add the -f flag:
docker system prune -a --volumes -fCleaning up individually
By using xargs --no-run-if-empty we can prevent Docker trying to stop or remove containers,
images, networks, or volumes if there are none to remove.
Stop all Docker containers and remove / delete them
docker ps -a -q | xargs --no-run-if-empty docker stop && docker ps -a -q | xargs --no-run-if-empty docker rmRemove / delete all Docker networks
docker network ls -q | xargs --no-run-if-empty docker network rmRemove / delete all Docker volumes
docker volume ls -q | xargs --no-run-if-empty docker volume rmRemove / delete all Docker images
docker images -q -a | xargs --no-run-if-empty docker rmiBash function to delete all Docker containers, volumes, networks, and images at once
This function will ask for confirmation before deleting the containers, volumes, and networks, and will ask again for the images (as you may want to keep these for development and not have to re-download them).
You could add this to your .bashrc file (e.g. nano ~/.bashrc) and reload with
source ~/.bashrc.
rmdocker() {
read -p $'\e[31mAre you sure you want to delete ALL Docker containers, volumes, and networks? [y/n]\e[0m\n' -n 1 -r
echo -e "\n"
if [[ $REPLY =~ ^[Yy]$ ]]
then
docker ps -a -q | xargs --no-run-if-empty docker stop
docker ps -a -q | xargs --no-run-if-empty docker rm
docker network ls -q | xargs --no-run-if-empty docker network rm
docker volume ls -q | xargs --no-run-if-empty docker volume rm
read -p $'\e[31mAre you sure you want to delete ALL Docker images as well? [y/n]\e[0m\n' -n 1 -r
echo -e "\n"
if [[ $REPLY =~ ^[Yy]$ ]]
then
docker images -q -a | xargs --no-run-if-empty docker rmi
fi
fi
}Frequently Asked Questions
Is this reversible?
No, deleting Docker containers, volumes, and images is permanent. While you can recreate containers from images and re-download images from registries, any data stored in volumes or containers will be permanently lost unless you have backups.
Will this affect running containers?
Yes, the Bash function first stops all running containers before deleting them. If you have critical services
running in Docker containers, this will cause downtime. The docker system prune command is safer
as it only removes stopped containers by default.
What's the difference between this and docker system prune?
The docker system prune command is the modern, recommended approach and is safer because it only
removes unused resources by default. It won't touch running containers or images that are in use. The manual
methods and Bash function shown here are more aggressive and will delete everything, including images you
might be actively using.