ThisCalculator

Everyday

How to Remove Docker Images and Free Up Disk Space Safely

Updated · Standard formula, shown below

How to Remove Docker Images and Free Up Disk Space Safely

This guide shows you how to remove Docker images and reclaim disk space without accidentally deleting containers, volumes, or data you still need. You’ll learn the exact commands to list, inspect, and delete images, plus how to use Docker’s built-in pruning tools safely. By the end, you’ll be able to clean up unused images and keep your Docker host lean.

What You’ll Need

  • A machine with Docker installed (Docker Engine 17.06 or later for most commands).
  • Command-line access (terminal on Linux/macOS, PowerShell or CMD on Windows).
  • Sufficient permissions to run Docker commands (usually your user is in the docker group, or you use sudo).
  • Basic familiarity with the terminal.

Step-by-Step

  1. Check disk usage. Run docker system df to see how much space images, containers, volumes, and build cache are using. This gives you a clear picture of what’s taking up space.
  2. List all images. Use docker images to see all images on your system. Note the IMAGE ID and REPOSITORY:TAG columns. You’ll need these to remove specific images.
  3. Identify images in use. Before deleting anything, check which images are used by running containers. Run docker ps -a to see all containers (including stopped ones). Note the IMAGE column. Do not delete an image that is referenced by a container you want to keep.
  4. Remove a specific image. To delete an image, use docker rmi . For example, docker rmi myapp:latest or docker rmi 3b5d2f2f9e3a. If the image is in use, you’ll get an error; you must stop and remove the container first (see Troubleshooting).
  5. Remove dangling images. Dangling images are untagged layers that are no longer referenced by any image. They often accumulate after rebuilding. Remove them with docker image prune. This is safe because they are not used by any container.
  6. Remove unused images (with caution). To remove all images not associated with a running container, use docker image prune -a. This will delete all images that are not used by at least one container. Be careful: this includes images that you might want to use later but are not currently running. Add --filter to exclude certain images, e.g., docker image prune -a --filter "until=24h" removes images older than 24 hours.
  7. Remove build cache. Docker build cache can consume significant space. Use docker builder prune to remove unused build cache. To remove all cache, add -a.
  8. Remove everything unused (the big cleanup). Run docker system prune -a to remove all stopped containers, unused networks, dangling images, and build cache. Add --volumes to also remove unused volumes (be very careful—volumes contain persistent data). This is a powerful command; it will prompt for confirmation.
  9. Verify the cleanup. Run docker system df again to see how much space you’ve reclaimed. It should show reduced usage.

Common Mistakes

  • Deleting images that are in use. This results in an error, but if you force delete with docker rmi -f, you might break running containers. Always stop and remove the container first.
  • Using docker system prune -a --volumes without checking. This deletes all unused volumes, which can contain database data or files you need. Always review what volumes exist with docker volume ls before pruning.
  • Ignoring the -a flag. Without it, docker image prune only removes dangling images. If you want to remove all unused images, you need -a.
  • Deleting images by name but having multiple tags. If you have multiple tags pointing to the same image ID, removing one tag only removes that tag, not the underlying image. To remove the image entirely, you must remove all tags or use the image ID.

Tips

  • Use filters to be precise. For example, docker image prune -a --filter "until=72h" removes images not used in the last 72 hours. This helps avoid deleting something you might need soon.
  • Regularly prune. Set a reminder to run docker system prune -f weekly to keep your system clean.
  • Use docker system df -v to get a detailed breakdown of space usage per image, container, and volume.
  • Label your images. When building, use meaningful tags like myapp:1.2.3 instead of latest to make it easier to identify what you can remove.
  • Consider using a registry. If you have many images, push them to a private registry and pull only when needed. This keeps your local disk clean.

Troubleshooting

  • Error: “image is being used by running container.â€� This means a container is using the image. Stop the container with docker stop , then remove it with docker rm . After that, you can remove the image.
  • Error: “image is being used by stopped container.â€� Even stopped containers hold a reference to the image. Remove the stopped container with docker rm first.
  • Permission denied when running Docker commands. Your user may not be in the docker group. Add yourself with sudo usermod -aG docker $USER and log out/in, or use sudo before each command.
  • Disk space not freed after removing images. Check if there are dangling volumes or build cache. Run docker system df to see what’s still using space. Use docker volume prune and docker builder prune to clean those.
  • You accidentally removed an image you need. If the image was pulled from a registry, you can re-pull it with docker pull . If it was built locally, you’ll need to rebuild it from your Dockerfile.

Frequently Asked Questions

What is the difference between docker rmi and docker image prune?

docker rmi removes a specific image by ID or name. docker image prune removes multiple unused images based on filters (dangling, older than a date, etc.).

Will removing images delete my containers?

No. Removing an image does not delete containers, but you cannot remove an image that is in use by a container. You must remove the container first if you want to delete the image.

Is it safe to run docker system prune -a?

It is safe if you understand that it removes all stopped containers, unused networks, dangling images, and build cache. It does not remove volumes unless you add --volumes. Always double-check that you don’t need any stopped containers.

How do I remove all images at once?

Use docker rmi $(docker images -q) to remove all images. This will fail if any image is in use. To force removal, add -f, but that can break running containers.

What are dangling images?

Dangling images are untagged layers that are no longer referenced by any image. They often result from rebuilding an image with the same tag. They are safe to remove.

Can I reclaim space from Docker Desktop on Windows/Mac?

Yes, the same commands work in the terminal. Additionally, Docker Desktop has a GUI option under Troubleshoot > Clean / Purge data, but the CLI commands are more precise.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.