Remove One Image
docker image ls
docker image rm nginx:1.30
docker rmi nginx:1.30 is the older short form of the same command. Name the image by repository and tag, or by the image ID from the first column of the list. You can give several names in one command.
An image can carry several tags. Removing one tag removes only that name; Docker deletes the layers when the last tag that points to them is gone.
Look Before You Delete
docker system df
docker image ls
docker ps -a
docker system df shows, for each type of object such as images, containers, and local volumes, the total, the number in use, the size, and how much is reclaimable. docker system df -v lists every item. docker ps -a shows which containers exist, running or stopped, and which image each one uses. To see how full the disk is overall, see how to check disk space in Linux.
When Docker Refuses: Image in Use
Docker does not delete an image that a container still uses, even a stopped container. The error says there is a conflict and names the container ID. Find and remove that container first:
docker ps -a --filter ancestor=nginx:1.30
docker rm old_web
docker image rm nginx:1.30
docker image rm -f forces the removal of the name even while a container uses the image. The layers stay on disk as long as any container, running or stopped, still uses them, so -f rarely saves space and leaves an untagged image behind.
Dangling Images
A dangling image has no tag. It is left behind when you build or pull a newer image under the same name. List and remove them:
docker image ls -f dangling=true
docker image prune
docker image prune removes dangling images only and asks for confirmation. Add -f to skip the question in scripts.
Remove All Unused Images
Read this before you run it: the next command deletes every image that no container uses. An image used by a stopped container counts as used and stays. Images you built on the server and never pushed to a registry are gone and must be rebuilt. Anything you start later downloads its image again.
docker image prune -a
To keep recent images, limit it by age:
docker image prune -a --filter "until=168h"
That removes unused images created more than seven days ago.
Remove Every Image
On a test machine you may want all images gone, including those of stopped containers. Remove the stopped containers first, then the images. Anything a stopped container wrote outside a volume is deleted with it, so check the list from docker ps -a first:
docker container prune
docker image rm $(docker image ls -q)
Images that running containers use stay, and Docker prints an error for each of them, as it does for an image ID with several tags. Another way to pass a list of IDs to a command is shown in the xargs guide.
docker system prune and Volumes
| Command | What it removes |
|---|---|
docker container prune | Stopped containers |
docker image prune | Dangling images |
docker image prune -a | All unused images |
docker builder prune | Build cache |
docker system prune | Stopped containers, unused networks, dangling images, build cache |
docker system prune -a also removes all unused images. Volumes are a different matter: they hold databases, uploads, and anything else a container writes. Since Docker Engine 23.0, docker system prune --volumes and docker volume prune remove anonymous volumes that no container uses. docker volume prune -a also removes every named volume without a container, and the database volume of a project you stopped with docker compose down is one of them. There is no undo. List the volumes first and back up any you want to keep:
docker volume ls
docker volume ls -f dangling=true
docker system prune --volumes
Keep It from Growing Again
- Prune on a schedule: a cron entry running
docker image prune -fweekly removes dangling images. See how to list cron jobs to check what is already scheduled. - Pin tags: fixed tags such as
nginx:1.30make it clear which images are still needed. - Clean up after updates: run
docker image pruneafter replacing containers with newer images.