How can I delete all local Docker images?
Categories:
How to Efficiently Delete All Local Docker Images

Learn various methods to remove all Docker images from your local machine, freeing up disk space and ensuring a clean development environment.
Managing Docker images is a crucial part of maintaining a clean and efficient development environment. Over time, your local Docker registry can accumulate numerous images, including old versions, dangling images, and those from various projects. This accumulation can consume significant disk space and sometimes lead to confusion. This article will guide you through different methods to delete all local Docker images, from simple commands to more advanced cleanup strategies.
Understanding Docker Image States
Before we dive into deletion, it's important to understand the different states of Docker images. Images can be actively used by containers, or they can be 'dangling' (untagged layers that are no longer referenced by any tagged image). Knowing this helps in deciding which images to target for removal.
flowchart TD A[Docker Image] --> B{Is it tagged?} B -- Yes --> C[Tagged Image] B -- No --> D{Is it referenced by another image?} D -- Yes --> E[Intermediate Layer] D -- No --> F[Dangling Image] C --> G[Used by Container?] E --> G G -- Yes --> H[Active Image] G -- No --> I[Unused Tagged Image]
Flowchart illustrating Docker image states and their relationships
Method 1: Deleting All Images (Forceful)
The most straightforward way to delete all Docker images is to use the docker rmi
command combined with docker images -q
. This method will attempt to remove all images, including those that are currently in use by stopped containers. If an image is in use by a running container, you might need to stop and remove the container first, or use the force flag.
docker rmi $(docker images -a -q)
Command to remove all Docker images, including intermediate and untagged ones.
docker rmi $(docker images -a -q)
will attempt to remove all images. If an image is used by a running container, this command will fail for that specific image unless you add the -f
(force) flag. Be cautious with the force flag as it can lead to unexpected behavior if you have containers you wish to keep running.Method 2: Deleting Dangling Images
Dangling images are layers that have no associated tags and are not used by any active images. They are often the result of building new images that replace older versions. These images are safe to remove and are a common source of wasted disk space.
docker image prune
Command to remove all dangling Docker images.
To remove all unused images (dangling and untagged images not associated with any container), you can use the --all
flag:
docker image prune --all
Command to remove all unused Docker images, including dangling and untagged ones.
Method 3: Using docker system prune
for Comprehensive Cleanup
For a more comprehensive cleanup, Docker provides the docker system prune
command. This command removes all stopped containers, all networks not used by at least one container, all dangling images, and optionally all build cache. This is often the most effective way to reclaim significant disk space.
docker system prune
Command to remove stopped containers, unused networks, and dangling images.
To also remove all unused images (not just dangling ones) and all build cache, add the --all
and --volumes
flags:
docker system prune --all --volumes
Comprehensive cleanup command to remove all unused Docker resources, including images and volumes.
docker system prune
command is powerful. It will ask for confirmation before proceeding. Always review the output to ensure you are not deleting anything critical.Step-by-Step: Cleaning Your Docker Environment
Here's a recommended sequence of steps for a thorough cleanup of your Docker environment.
1. Step 1: Stop All Running Containers
Before removing images, it's best practice to stop any running containers that might be using them. This prevents errors and ensures a clean removal process.
docker stop $(docker ps -a -q)
2. Step 2: Remove All Stopped Containers
Once stopped, remove all containers. This frees up resources and allows images to be deleted.
docker rm $(docker ps -a -q)
3. Step 3: Perform a System Prune
Execute a comprehensive system prune to remove dangling images, unused networks, and optionally volumes and build cache.
docker system prune --all --volumes
4. Step 4: Verify Image Removal
After running the prune command, verify that all desired images have been removed.
docker images -a
This command should now show an empty or significantly reduced list of images.