
Docker lets you build, ship, and run applications in lightweight containers. However, over time, your system can become clogged with unused images from builds, tests, or outdated versions that consume disk space and make container management harder.
This article shows how to remove all Docker images from your system using the latest Docker CLI commands. It also explains the difference between removing and pruning Docker images, and highlights a few tips to avoid common cleanup mistakes.
The Short Answer
# List all images
$ docker image ls
# Remove dangling images (untagged and unused images)
$ docker image prune -f
# Remove all unused images forcefully and without prompt
$ docker image prune -a -f
Commands to Remove Docker Image
Docker offers several commands for removing system resources, which serve different purposes.
docker image remove [IMAGE...]
ordocker image rm [IMAGE...]
: Deletes one or more images by ID or tag. Useful for manual cleanup.docker image prune
: Removes dangling images (images that are both untagged and unused).
Difference Summary
Command | Affects | Description |
---|---|---|
docker image rm |
Images | Removes specific images, including referenced ones if forced |
docker image prune |
Images | Removes only dangling (untagged and unused) images |
Remove All Docker Images
Before you remove anything, list running containers and images to see the present state.
console$ docker container ls $ docker image ls
You cannot delete an image that’s in use by a container. You need to first:
Stop all containers.
console$ docker container stop $(docker container ls -aq)
Remove all containers.
console$ docker container prune -f
Confirm the removal of containers.
console$ docker container ls -a
The output should contain no containers.
Now you can remove all images by referencing their image IDs.
console$ docker image prune -a -f
Optionally, clean up all the unused Docker resources, including volumes and networks.
console$ docker system prune -a -f
Common Errors and Debugging
Error | Cause | Solution |
---|---|---|
Cannot remove image... being used | Image still used by container | Stop and remove the container |
Permission denied | Insufficient privileges | Use sudo or add user to docker group |
No such image | Image already deleted or mistyped | Use docker image ls to verify image ID |
Best Practices for Removing Images in Docker
Ensure to tag critical images to avoid accidental deletion.
Schedule cleanup jobs using
cron
or automation tools.Use the command below to check space usage:
console$ docker system df
Use labels and metadata to organize images and automate cleanup logic in CI/CD pipelines.
Before running
docker system prune
, consider exporting important volumes or configurations.
Conclusion
In this article, you learned how to remove all Docker images from your system using Docker CLI commands. You also explored the differences between docker image remove
and docker image prune
, and reviewed tips for avoiding common cleanup mistakes. For more information, refer to the official docker image documentation.
No comments yet.