Remove Docker images and reclaim local disk space
Choose image pruning or an intentional full reset, account for containers and volumes, and verify reclaimed storage.
To remove every local image, first decide what to do with containers that reference those images. For routine cleanup, docker image prune -a is usually a narrower choice: it removes images not referenced by a container while retaining those still needed by existing containers.
Inspect before removing resources
docker context show
docker system df -v
docker container ls -a
docker image ls
Confirm the selected daemon, especially when using remote contexts. Preserve databases, uploads, container-layer files, and any locally built image you cannot reproduce.
For unused images only:
docker image prune -a
Without -a, image pruning targets dangling images. A <none> display by itself should not replace checking whether an image is referenced or required.
Intentionally remove all containers and images
For a disposable daemon where every container may be removed, this Bash example handles an empty list without invoking an invalid removal command:
container_ids=$(docker container ls -aq) || exit 1
if [ -n "$container_ids" ]; then
docker container rm -f $container_ids || exit 1
fi
image_ids=$(docker image ls -aq | sort -u)
if [ -n "$image_ids" ]; then
docker image rm -f $image_ids
fi
The unquoted expansions here are deliberate lists of Docker-generated IDs, not arbitrary filenames. -f container removal stops running work abruptly. The example omits container -v so it does not also request anonymous-volume removal. Image deletion is not volume deletion, and a stopped container can still keep an image in use. Force-removal details vary with the references involved; do not infer that all referenced images are categorically impossible to remove.
In PowerShell, a pipeline is convenient:
docker container ls -aq | ForEach-Object { docker container rm -f $_ }
docker image ls -aq | Sort-Object -Unique | ForEach-Object { docker image rm -f $_ }
PowerShell does support $() subexpressions; its argument and array behavior differs from a POSIX shell.
Broader pruning is a different operation
docker system prune -a also removes stopped containers, unused networks, and unused build cache. It retains running containers and their required images. Its --volumes option additionally prunes unused anonymous volumes in current Docker; do not describe it as removing every unused named volume.
docker builder prune -a targets unused build cache for the relevant builder. Losing cache can make the next build slower even when running applications remain unaffected.
FAQ
How can I retain selected images?
Remove explicit reviewed references instead of subtracting partial IDs with a text filter. Save irreplaceable images before cleanup.
Why is less space freed than expected?
Layers can be shared, and other builders, volumes, or logs may account for the remaining usage. Recheck docker system df -v and the filesystem hosting Docker's data.
Should cleanup run automatically in CI?
Only under a policy that accounts for concurrent jobs, retained artifacts, and cache needs. A blind periodic system prune is not suitable for every shared builder.
For collected host metrics, Guance's disk integration can track filesystem utilization; it does not identify which Docker resource is safe to delete.
See system prune and image prune.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan