0
0
DockerHow-ToBeginner · 3 min read

How to Prune Unused Images in Docker Quickly and Safely

Use the docker image prune command to remove all unused Docker images. Add the -a flag to delete all images not currently used by containers, freeing up disk space safely.
📐

Syntax

The basic command to remove unused Docker images is docker image prune. You can add options to control what gets removed:

  • -a: Remove all unused images, not just dangling ones.
  • -f: Force removal without confirmation prompt.
bash
docker image prune [-a] [-f]
💻

Example

This example shows how to remove all unused Docker images without confirmation. It frees up space by deleting images not used by any container.

bash
docker image prune -a -f
Output
Deleted Images: untagged: example/image:latest Deleted image sha256:abc123... Total reclaimed space: 500MB
⚠️

Common Pitfalls

One common mistake is running docker image prune without the -a flag, which only removes dangling images (those without tags). This might not free as much space as expected.

Another pitfall is not using -f and missing the confirmation prompt, which can interrupt automated scripts.

bash
docker image prune
# Only removes dangling images

docker image prune -a -f
# Removes all unused images without prompt
📊

Quick Reference

CommandDescription
docker image pruneRemove dangling images only
docker image prune -aRemove all unused images
docker image prune -fForce removal without confirmation
docker image prune -a -fRemove all unused images without prompt

Key Takeaways

Use docker image prune -a to remove all unused images, not just dangling ones.
Add -f to skip confirmation and automate pruning.
Pruning frees disk space by deleting images not used by any container.
Without -a, only dangling images are removed, which may not free much space.
Always check which images will be removed to avoid deleting needed images.