How to remove old and unused Docker images
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Docker is a powerful platform for building, running, and managing containers. Over time, as you pull and build more Docker images, your system can get cluttered with old and unused images, taking up valuable disk space. Managing Docker images efficiently is crucial for maintaining a clean and effective development environment. Here’s a guide on how to find and remove those old and unused Docker images.
Understanding Docker Images and Containers
Before proceeding, it’s important to understand what Docker images and containers are:
- Docker Image: A Docker image is a read-only template used to create containers. Images are built by executing a set of instructions from a Dockerfile.
- Docker Container: Containers are runnable instances of images. You can start, stop, move, and delete containers using Docker's commands.
When you build images frequently, Docker stores every state as a layer. These layers are shared across different images to optimize disk space. However, unused images, especially those not tagged and called "dangling images," still consume disk space.
How to List Docker Images
To start cleaning up, you first need to see what's on your system. Use the docker images command:
This command lists all the Docker images on your system, including their repository, tag, image ID, creation time, and size.
Finding Dangling and Unused Images
Dangling images are layers that have no relationship to any tagged images. They typically occur when a new build of an image takes place and the old layers are no longer used.
List all dangling images:
For unused images, which might not be associated with any container, Docker 17.06.0 and higher versions simplify the process through the docker image prune command.
Removing Images
Here’s how you can remove Docker images:
Remove a single image:
Use the image ID from docker images list:
Remove multiple images: You can chain image IDs:
Remove dangling images: This command removes all the dangling images:
Remove all unused images (not just dangling):
This command will prompt you to confirm deletion of each image that is not associated with a container.
Automating Cleanup
You can automate this process by incorporating cleanup commands into scripts, which can be run manually or scheduled via cron jobs (on Linux/Mac) or Task Scheduler (on Windows).
Understanding Image Dependencies
One critical aspect is understanding dependencies. Removing an image that forms the base for multiple other containers can lead to unexpected problems. Always ensure that the image is not required by any running or stopped containers. Check this by:
This command shows all containers, and you need to ensure no container is using the image ID you plan to delete.
Summary Table
| Command | Use Case | Description |
docker images | Viewing images | Lists all Docker images on the system. |
docker images -f dangling=true | Finding dangling images | Lists all dangling images that are no longer tagged or associated with a container. |
docker rmi [IMAGE ID] | Removing specific images | Deletes the image with the specified ID. |
docker image prune | Removing dangling images | Deletes all dangling images from the system. |
docker image prune -a | Removing all unused images | Deletes all images not associated with any containers. |
Final Thoughts
Regular maintenance of Docker images and containers is essential. It not only helps in recovering disk space but also ensures that your Docker environment is organized and efficient. Always proceed with caution before permanently removing any data, and consider backing up important images if necessary.

