Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Delete All Docker Images: Safe Ways & What Happens

· 6 min read · Home Tech

In this article
  1. What Docker Images Actually Are
  2. Why You Might Want to Delete All Images
  3. The Main Commands: What Each One Does
  4. What Happens When You Delete an Image?
  5. How to Choose the Right Cleanup Method
  6. Common Mistakes and Pitfalls
  7. Alternatives: Selective Cleanup and Automation
  8. Frequently Asked Questions

Yes, you can delete all Docker images on your system, and the standard command is docker system prune -a (or docker rmi -f $(docker images -q) for a more direct approach). This permanently removes every unused image from your local Docker daemon, freeing disk space but requiring you to re-pull any images you need later.

What Docker Images Actually Are

A Docker image is a read-only template with instructions for creating a container. It contains the application code, runtime, libraries, and system tools needed to run software in an isolated environment. Images are built in layers, and each layer is cached. When you pull an image from a registry like Docker Hub, you download all its layers to your local machine.

Images are not the same as containers. A container is a running instance of an image, with its own writable layer on top. Deleting an image does not affect running containers (they keep using the image layers they started from), but it can affect stopped containers if you try to restart them later.

Why You Might Want to Delete All Images

The most common reason is disk space. Docker images can be large – a typical Linux image is 100–500 MB, and development images with build tools can be several gigabytes. Over time, old versions, intermediate build layers, and dangling images (untagged ones left after builds) accumulate. Running docker system df shows you how much space images, containers, volumes, and build cache are using.

Other reasons include:

  • Cleaning a development machine before a fresh start.
  • Removing sensitive data that might be baked into an image.
  • Freeing inodes on filesystems with limited inode counts.
  • Preparing a demo environment to avoid pulling the wrong image.

Deleting all images is a blunt tool. If you only need to free space, consider removing unused images (not all) or just dangling ones.

The Main Commands: What Each One Does

Docker provides several ways to delete images, and they differ in scope:

  • docker image prune – removes dangling images (untagged ones). Safe and quick.
  • docker image prune -a – removes all unused images, not just dangling. Unused means not referenced by any container (running or stopped).
  • docker system prune -a – removes all unused images, containers, networks, and build cache. This is the “nuke” option.
  • docker rmi $(docker images -q) – forces removal of every image, even if a container references it (with -f).

Note that docker system prune -a will ask for confirmation before deleting. You can add -f to skip the prompt. It does not delete volumes by default (you need --volumes for that).

What Happens When You Delete an Image?

When you delete an image, Docker removes its layers from the local storage. If multiple images share layers (which is common because of caching), those layers are only removed when no remaining image references them. So deleting one image might not free as much space as you expect if another image shares its base layers.

Running containers are unaffected – they keep using the image layers they were started from. Stopped containers can still be inspected, but if you try to start one whose image has been deleted, Docker will attempt to re-pull the image from the registry. If the image is not available (e.g., a local build that was never pushed), the container will fail to start.

Docker caches build layers separately. Deleting images does not clear the build cache. To clear that, use docker builder prune.

How to Choose the Right Cleanup Method

Start by checking what you actually have. Run docker images to list images and docker ps -a to list all containers (including stopped ones). Then decide:

  • If you want to keep images used by stopped containers, use docker image prune (only dangling).
  • If you want to remove everything not needed by any container, use docker image prune -a.
  • If you want to wipe everything (images, containers, networks, cache), use docker system prune -a.
  • If you want to delete literally every image, even those used by containers, use docker rmi -f $(docker images -q) – but this is rarely needed and can break containers.

A safe routine is: stop and remove containers you no longer need (docker rm $(docker ps -aq)), then run docker image prune -a. This frees most space without forcing anything.

Common Mistakes and Pitfalls

One common mistake is thinking docker rmi $(docker images -q) will fail if there are running containers. It will fail unless you add -f, which forces removal. Forcing is dangerous because it can leave containers in a broken state.

Another mistake is ignoring build cache. After deleting images, you might still see low disk space because the build cache can be huge. Use docker system df to see the breakdown.

Also, be aware that docker system prune -a will delete all unused containers, not just images. If you have stopped containers you want to keep, do not use this command without checking the list first.

Finally, remember that deleting an image is permanent. There is no undo. If you need an image later, you must pull it again (which requires internet) or rebuild it from your Dockerfile.

Alternatives: Selective Cleanup and Automation

Instead of deleting everything, you can target specific images:

  • Remove a single image: docker rmi <image_id>
  • Remove all images older than a week: docker image prune -a --filter "until=168h"
  • Remove images by label: docker rmi $(docker images -q --filter "label=keep=false")

For regular maintenance, you can set up a cron job (Linux) or scheduled task (Windows) to run docker system prune -f weekly. This removes dangling images and stopped containers without touching used images.

If you are using Docker Desktop, it has a built-in “Clean up” feature in the Troubleshoot menu that does a similar cleanup.

Frequently Asked Questions

Will deleting all images delete my containers?

No, deleting images does not delete containers. However, docker system prune -a will delete all stopped containers as part of its cleanup. If you want to keep containers, use docker image prune -a instead.

How do I delete all images but keep volumes?

Use docker system prune -a --volumes if you want to delete volumes too. To keep volumes, omit the --volumes flag. By default, docker system prune does not delete volumes.

What is the difference between dangling and unused images?

A dangling image is one that has no tag and is not referenced by any container – usually an intermediate layer from a build. An unused image is any image not referenced by a container, including tagged ones you pulled but no longer use. docker image prune removes only dangling; -a removes all unused.

Can I recover a deleted Docker image?

No, there is no built-in way to recover a deleted image. You must re-pull it from a registry or rebuild it from your Dockerfile. If the image was only local and you have no Dockerfile, it is gone permanently.

Why does deleting images not free as much space as expected?

Because images share layers. If you delete one image, its base layers might still be used by another image. Also, the build cache and logs can take significant space. Check docker system df to see what is actually using space.

Is it safe to delete all images on a production server?

Only if you are certain you can re-pull or rebuild every image you need. On a production server, it is safer to remove only unused images (docker image prune -a) and never use -f with rmi. Consider using a registry for your own images so they can be re-pulled.