Skip to main content

How to clear your Docker cache

Clear cache

Prune the builder cache first.

docker builder prune

Go deeper and prune the system cache. This frees up a lot of space.

docker system prune

Remove containers

Remove unnecessary containers to free up more space.

Exited containers

docker ps -q -f "status=exited" lists all exited containers. Combine this command with docker rm to remove them.

docker rm $(docker ps -q -f "status=exited")

All containers

Danger zone

Remove all containers.

docker rm $(docker ps -a -q)

Remove images

Docker images and their versions can pile up quickly. Remove unnecessary images to free up space.

Dangling images

Remove dangling images - unused images with no names or tags.

docker rmi $(docker images -f "dangling=true" -q)

Matching images

Remove images with a certain name or label using a pattern.

export PATTERN="*image_name*"

Use docker rmi directly...

docker rmi $(docker images -f "reference=${PATTERN}" -q)

...or pipe them to docker rmi.

docker images -a | grep ${PATTERN} | awk '{print $3}' | xargs docker rmi

All images

Danger zone

Remove all images.

docker rmi $(docker images -a -q)

References

  1. "How can I delete Docker images by tag, preferably with wildcarding?" Stack Overflow.
  2. "How to clear Docker cache and save disk space" Reeshma Mathews.
  3. "How To Delete All Docker Images" Will Stanton.