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
dockergroup, or you usesudo). - Basic familiarity with the terminal.
Step-by-Step
- Check disk usage. Run
docker system dfto see how much space images, containers, volumes, and build cache are using. This gives you a clear picture of what’s taking up space. - List all images. Use
docker imagesto see all images on your system. Note theIMAGE IDandREPOSITORY:TAGcolumns. You’ll need these to remove specific images. - Identify images in use. Before deleting anything, check which images are used by running containers. Run
docker ps -ato see all containers (including stopped ones). Note theIMAGEcolumn. Do not delete an image that is referenced by a container you want to keep. - Remove a specific image. To delete an image, use
docker rmi. For example,docker rmi myapp:latestordocker rmi 3b5d2f2f9e3a. If the image is in use, you’ll get an error; you must stop and remove the container first (see Troubleshooting). - 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. - 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--filterto exclude certain images, e.g.,docker image prune -a --filter "until=24h"removes images older than 24 hours. - Remove build cache. Docker build cache can consume significant space. Use
docker builder pruneto remove unused build cache. To remove all cache, add-a. - Remove everything unused (the big cleanup). Run
docker system prune -ato remove all stopped containers, unused networks, dangling images, and build cache. Add--volumesto also remove unused volumes (be very careful—volumes contain persistent data). This is a powerful command; it will prompt for confirmation. - Verify the cleanup. Run
docker system dfagain 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 --volumeswithout checking. This deletes all unused volumes, which can contain database data or files you need. Always review what volumes exist withdocker volume lsbefore pruning. - Ignoring the
-aflag. Without it,docker image pruneonly 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 -fweekly to keep your system clean. - Use
docker system df -vto get a detailed breakdown of space usage per image, container, and volume. - Label your images. When building, use meaningful tags like
myapp:1.2.3instead oflatestto 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 withdocker 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 rmfirst. - Permission denied when running Docker commands. Your user may not be in the
dockergroup. Add yourself withsudo usermod -aG docker $USERand log out/in, or usesudobefore each command. - Disk space not freed after removing images. Check if there are dangling volumes or build cache. Run
docker system dfto see what’s still using space. Usedocker volume pruneanddocker builder pruneto 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.