Docker Community Forums

Share and learn in the Docker community.

How to delete cache?

Is there a way to delete the Docker cache?

Last time I checked, there wasn’t a specific docker command for this

I use a few useful alias

alias docker_clean_images='docker rmi $(docker images -a --filter=dangling=true -q)'
alias docker_clean_ps='docker rm $(docker ps --filter=status=exited --filter=status=created -q)'

The first one cleans all dangling images. This is useful for removing intermediate images left over from multiple builds. The second one is for removing stopped containers. These are aliases I use for routine maintenance

If you want to remove ALL of your cache, you first have to make sure all containers are stopped and removed, since you cannot remove an image in use by a container. So something similar

docker kill $(docker ps -q)
docker rmi $(docker images -a -q)

This would kill and remove all images in your cache. Less recommended, you could wipe the /var/lib/docker dir and start docker over, but that’s hardly necessary just to clear the cache

docker system prune -a

wish it works


This will delete everything not only cached intermittent images layers but also, images that aren’t connected to at least one container and all exited containers.

is there any command to delete only cached image layers?

1 Like

I pull git repository in my production-ready image.

RUN git clone --depth=1 --branch=${BRANCH} ${REPOSITORY} .

Before this step I have really heavy steps (1 hour of OpenCV coompilation)
But if I push some changes to git then I need to add some invalidation step. It’s really messy.

I found good solution to use the LABEL. I put a git ref value in the label. It’s even bring real benefits also.
But(!) I specified the label wrongly at latest build

# instead of 

Docket didn’t set the HASH_COMMIT to labels. (I check via docker inspect)
But Docker anyway use cache if I fix it!

I don’t want to do docker system prune -a
It will remove different heavy images too!

I have a lot of pain with it :frowning_face:

If you have a heavy cache which you want to save. f.e. 1 hour of OpenCV coompilation
or other images which you don’t wanna remove. Then it’s big pain

1 Like
docker builder prune

it remove build cache
for more info click


This did the trick for me.


1 Like

What’s the strategy for leaning up old images? If you’ve been running docker-compose build without --no-cache for a while, then images all the way back are based on each other… So I’ll eventually run out of disk space. What’s the method for keeping the last ~15 images when always using “docker-compose build”?

Works great, thanks!