docker
container management
docker images
image deletion
devops

How to delete a docker image?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Deleting a Docker image is usually a simple docker rmi command, but what actually happens depends on whether the image is tagged multiple ways and whether any containers still reference it. The command removes the local image reference, not a remote image in a registry.

That is why image deletion sometimes works immediately and sometimes fails with a message saying the image is being used. The fix is to understand what Docker is protecting and remove dependencies in the right order.

Delete By Name Or ID

The basic commands are:

bash
docker rmi myapp:latest

or:

bash
docker rmi a6b73c47c7c1

Both forms remove the local image reference. Using the image ID is helpful when multiple tags point to the same underlying image data.

Check What You Are Removing

List images first:

bash
docker images

That shows repository, tag, image ID, and size. It is a good habit before deletion because it helps you confirm whether you are deleting a specific tag or a shared image used by several tags.

Why Docker Sometimes Refuses

Docker usually refuses to remove an image when a container still depends on it, even if that container is stopped. In that case, remove the container first:

bash
docker ps -a

docker rm my-container

Then try the image deletion again:

bash
docker rmi myapp:latest

This protection is useful because deleting the image while containers still depend on it would make the local state inconsistent.

Forced Deletion

If you really need to remove the image reference anyway, use force mode:

bash
docker rmi -f myapp:latest

Use -f carefully. It is helpful during local cleanup, but it can hide the fact that multiple tags or local containers were still tied to that image.

Remove Dangling And Unused Images

If the real goal is cleanup rather than a single image, Docker also supports pruning:

bash
docker image prune

That removes dangling images, meaning layers that are no longer tagged.

For a broader cleanup:

bash
docker system prune

That command affects more than images, so read the prompt carefully before accepting it.

Images, Tags, And Layers

Deleting an image tag does not necessarily delete every underlying layer immediately. Docker keeps layer data that is still referenced by other tags or images. That is why disk usage and image listings can be a little surprising until you understand that tags are names, while image data may be shared underneath.

Removing Many Images At Once

If you really are cleaning up a development machine, it is common to combine docker images with filters and prune commands rather than deleting one tag at a time. The important part is to distinguish targeted cleanup from broad cleanup so you do not remove cached layers you still wanted for active work.

Common Pitfalls

  • Thinking docker rmi deletes an image from a remote registry.
  • Forgetting that stopped containers can still block image removal.
  • Confusing a tag with the underlying image ID.
  • Using -f before checking what else depends on the image.
  • Running docker system prune when you only intended to remove one local image.

Summary

  • Use docker rmi <image> to delete a local Docker image.
  • Delete dependent containers first if Docker reports the image is in use.
  • Use image IDs when tags are ambiguous.
  • Use docker image prune or docker system prune for broader cleanup.
  • Force deletion exists, but it should be used deliberately.

Course illustration
Course illustration

All Rights Reserved.