How can I safely remove old and unused Docker images?

I want to clean up disk space by removing all unused Docker images, including both untagged ones and those pulled long ago with specific tags.

What’s the recommended way to docker prune images without affecting running containers or important builds?

I usually run this to clean up just unused images (untagged, dangling):

 docker image prune

If I want to go deeper and remove all unused images, even ones with tags (but not in use by containers),

I use:

docker image prune -a`Preformatted text`

But I always double-check which images are still in use by containers using docker ps -a before doing that.

It’s safe if your containers are running off newer builds.

I built a small script that shows me all images first with:

docker images

Then I use:


docker system prune --all --volumes

But I only use --volumes when I know I’m not actively using named volumes.

The interactive prompt helps avoid accidents. I also label important images and avoid pruning those using --filter if needed.

Before running any prune command, I go through and re-tag any images I want to keep:

docker tag <image_id> mybackup:<tag>

Then I use docker image prune -a for the rest.

That way, anything I do want to keep stays safe. It’s especially useful when I haven’t touched some projects in a while but might come back to them.