Delete Docker Image
To delete a Docker image properly, first make sure no container is using that image. If a stopped or running container was created from the image, remove that container first, and then remove the image with docker rmi or docker image rm.
This tutorial shows the complete flow: find the container that blocks image deletion, stop it if required, remove the container, and then delete the Docker image safely.
- Why Docker image deletion fails with a conflict error
- Stop the container created from the image
- Remove the container before deleting the image
- Remove Docker image with docker rmi
- When to use force delete for a Docker image
Why Docker image deletion fails with a conflict error
Sometimes, when you run a docker image and then try to delete it, you might get an error message similar to following.
Error response from daemon: conflict: unable to delete 2867b9f04038 (must be forced) - image is being used by stopped container caff7e1e3508

The error means Docker has found a container that still references the image. In the message above, 2867b9f04038 is the image id and caff7e1e3508 is the stopped container id. Docker does not remove the image normally until that container is removed.
We shall present you a step-by-step guide to delete a docker image properly.
Check Docker image and container references before deletion
Before deleting an image, it is useful to list the image and the containers created from it. This avoids deleting the wrong image, especially when multiple tags or old containers exist on the same system.
docker images
docker ps -a
To check containers created from a specific image name, use the ancestor filter.
docker ps -a --filter ancestor=java-application
Replace java-application with your image name or image id. If Docker shows one or more containers, remove those containers before deleting the image.
1 Stop the container before deleting Docker image
You can get the container id from the error message. “image is being used by stopped container caff7e1e3508“
If the container is already stopped, the docker stop command is not required. If the container is running, stop it first.
Following is the syntax to stop the container.
$ docker stop <containerid>
Example
root@arjun-VPCEH26EN:/home/arjun/workspace/docker# docker stop caff7e1e3508
caff7e1e3508
The container is stopped and echoes back with the container id.
2 Remove the container that uses the Docker image
Once the container is stopped, we have to remove the container.
Following is the syntax to remove the container.
$ docker rm <containerid>
Example
root@arjun-VPCEH26EN:/home/arjun/workspace/docker# docker rm caff7e1e3508
caff7e1e3508
If more than one stopped container was created from the same image, remove each blocking container. You can remove all stopped containers only when you are sure they are no longer needed.
docker container prune
The prune command removes stopped containers after confirmation. It does not remove running containers.
3 Remove Docker Image
Once the container is removed, we are good to go with removing Docker Image.
Following is the syntax to remove the image.
$ docker rmi <imageid>
You can also use the equivalent Docker image command.
docker image rm <imageid>
Example
root@arjun-VPCEH26EN:/home/arjun/workspace/docker# docker rmi 2867b9f04038
Untagged: java-application:latest
Deleted: sha256:2867b9f04038b6a8e4ffdad800d4ea9ad7fa8ba1ea5fd4e967e7731ef615199d
Deleted: sha256:d858962818586481bb955bb5791e775e37411b0f9e870fe9826858928c53410a
Deleted: sha256:a81438c31d4f1ec303b4872cabe239907e8e8cecf6787505bb28085c20b25f82
root@arjun-VPCEH26EN:/home/arjun/workspace/docker#
The docker image is removed successfully.
Delete one Docker image by image name or tag
You do not always need the image id. If the image has a clear repository name and tag, you can delete it by name and tag.
docker rmi java-application:latest
If an image has multiple tags pointing to the same image id, removing one tag may only untag the image. Docker deletes the underlying image layers only after no tag or container reference needs them.
Force delete Docker image only when the reference is understood
Docker may suggest that an image “must be forced”. The force option can remove an image reference even when Docker detects a conflict, but it should not be the first choice if you have not checked which container is using the image.
docker rmi -f <imageid>
Use -f only when you are certain the image is not needed by a container you plan to keep. For normal cleanup, remove the container first and then remove the image without force.
Remove dangling and unused Docker images
A dangling image is an image layer that is not tagged, often shown as <none> in the image list. To remove dangling images after builds, use image prune.
docker image prune
To remove unused images that are not referenced by containers, use the -a option carefully.
docker image prune -a
Do not run prune commands on a shared development or production machine unless you understand which images and containers are still required.
Docker image delete command reference
| Task | Command | Use when |
|---|---|---|
| List images | docker images | You need the image id, name, or tag. |
| List all containers | docker ps -a | You need to find containers that may be using the image. |
| Stop a running container | docker stop <containerid> | The container is still running. |
| Remove a stopped container | docker rm <containerid> | The container blocks image deletion. |
| Delete one image | docker rmi <imageid> | No container is using the image. |
| Delete image by tag | docker rmi repository:tag | You know the exact image name and tag. |
| Remove dangling images | docker image prune | You want to clean untagged image layers. |
Docker image deletion FAQs
How do I fully delete a Docker image?
First remove any containers created from that image, including stopped containers. Then run docker rmi <imageid> or docker image rm <imageid>. If the image has multiple tags, remove all tags that point to the same image id.
Can I safely delete Docker images?
Yes, if no required container or future build depends on the image. Check with docker ps -a and docker images before deleting. On shared systems, confirm that another project is not using the same image.
How do I delete one image in Docker?
Use docker rmi <imageid> or docker rmi repository:tag. If Docker reports that a stopped container is using the image, remove that container first with docker rm <containerid>.
What is the difference between docker rm and docker rmi?
docker rm removes containers. docker rmi removes images. If a container was created from an image, remove the container with docker rm before deleting the image with docker rmi.
Does deleting a Docker image delete volumes?
No. Removing an image does not remove Docker volumes. Volumes are managed separately. Check volumes with docker volume ls before removing any stored data.
Editorial QA checklist for deleting Docker image properly
- The tutorial separates container removal with
docker rmfrom image removal withdocker rmi. - The conflict error is explained using both the image id and container id from the message.
- The force delete option is presented as a careful fallback, not the default method.
- The prune commands are clearly limited to dangling or unused images and include a caution for shared systems.
Conclusion for deleting a Docker image properly
In this Docker Tutorial – Delete Docker Image, we have successfully removed the image from Docker’s images.
The safest order is: identify the image, find containers using it, stop the running container if needed, remove the container, and then delete the Docker image.
TutorialKart.com