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:

Basic cleanup
docker system prune

To also remove all unused images and volumes, add the -a and --volumes flags:

Comprehensive cleanup
docker system prune -a --volumes

To skip the confirmation prompt, add the -f flag:

Force cleanup
docker system prune -a --volumes -f

Cleaning 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

Stop and remove containers
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

Remove networks
docker network ls -q | xargs --no-run-if-empty docker network rm

Remove / delete all Docker volumes

Remove volumes
docker volume ls -q | xargs --no-run-if-empty docker volume rm

Remove / delete all Docker images

Remove 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 (e.g. nano ~/.bashrc) and reload with source ~/.bashrc.

rmdocker
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.