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 prune
To also remove all unused images (not just dangling ones) and volumes, add the -a and --volumes flags:
docker system prune -a --volumes
To skip the confirmation prompt, add the -f flag:
docker system prune -a --volumes -f
Cleaning up individually
By using xargs --no-run-if-empty we can prevent Docker trying to stop containers 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 rm
Remove / delete all Docker networks
docker network ls -q | xargs --no-run-if-empty docker network rm
Remove / delete all Docker volumes
docker volume ls -q | xargs --no-run-if-empty docker volume rm
Remove / delete all Docker images
docker images -q -a | xargs --no-run-if-empty docker rmi
Bash 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 (eg. nano ~/.bashrc) so you always have it to use. Remember after adding it to the .bashrc file to source it to load in your changes:
. ~/.bashrc
or
source ~/.bashrc
Here's the Bash function:
rmdocker() {read -p $'\e[31mAre you sure you want to delete ALL Docker containers, volumes, and networks? [y/n]\e[0m\n' -n 1 -recho -e "\n"if [[ $REPLY =~ ^[Yy]$ ]]then# Stop all containersdocker ps -a -q | xargs --no-run-if-empty docker stop# Delete all containersdocker ps -a -q | xargs --no-run-if-empty docker rm# Delete all networksdocker network ls -q | xargs --no-run-if-empty docker network rm# Delete all volumesdocker volume ls -q | xargs --no-run-if-empty docker volume rmread -p $'\e[31mAre you sure you want to delete ALL Docker images as well? [y/n]\e[0m\n' -n 1 -recho -e "\n"if [[ $REPLY =~ ^[Yy]$ ]]then# Delete all imagesdocker images -q -a | xargs --no-run-if-empty docker rmififi}
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. Always ensure you have backups of important data before running cleanup commands.
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. Use the docker ps command first to check what's running before proceeding with cleanup.
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. Use docker system prune -a --volumes for most cleanup needs, and only use the comprehensive Bash function when you want to completely reset your Docker environment.