Deleting Unused Docker Images, Containers, and Volumes
Regularly deleting unused Docker resources such as images, containers, and volumes can help save storage space and keep the Docker environment clean.
Bernd Fischer
18. Oct 2024 · 3 min read
Introduction
When working with Docker, unused images, containers, and volumes often accumulate and consume storage space. It is important to regularly remove these unused resources to free up storage and maintain a clean Docker environment. Using the commands docker image prune, docker container prune, docker volume prune, and docker system prune, you can efficiently clean up these unused resources.
Deleting Docker Images
Displaying Unused Docker Images
You can list all Docker images that are currently not used by any running or stopped container with the following command:
docker images -f "dangling=true"
These "dangling" images are untagged images that are no longer needed.
Deleting Unused Docker Images
To delete all unused images at once:
docker image prune
This command deletes all unused (dangling) images that are not associated with any container.
Deleting All Unused Images
If you want to delete all unused images, including those with tags, you can use the following command:
docker image prune -a
This command removes all images not used by any container, regardless of whether they have a tag or not.
Deleting Docker Containers
Displaying Stopped Docker Containers
You can display all stopped containers with this command:
docker ps -a
This command shows a list of all containers, both running and stopped.
Deleting Stopped Docker Containers
To remove all stopped Docker containers that are no longer needed:
docker container prune
This deletes only the stopped containers that are no longer required. Running containers remain intact.
Deleting Docker Volumes
Displaying Unused Docker Volumes
Docker volumes are used to share data between containers. Sometimes volumes remain after a container is stopped or even deleted. To list unused volumes:
docker volume ls -f "dangling=true"
This lists all unused volumes that are no longer linked to any containers.
Deleting Unused Docker Volumes
To delete all unused Docker volumes:
docker volume prune
This deletes all volumes that are no longer used by any container.
Deleting Everything at Once (Images, Containers, and Volumes)
If you want to perform a comprehensive cleanup of unused Docker resources, you can delete unused images, containers, networks, and volumes simultaneously with the following command:
docker system prune
This command removes:
- All unused images
- All stopped containers
- All unused volumes
- All unused networks
Caution: Deleting Volumes with docker system prune --volumes
By default, docker system prune does not delete unused volumes. To also delete volumes, add the flag --volumes:
docker system prune --volumes
This deletes all unused images, containers, networks, and volumes that are no longer associated with running containers.