Docker Community Forums

Share and learn in the Docker community.

How to delete cache?


(Irfuel) #1

Is there a way to delete the Docker cache?


(Andy Neff) #2

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_clean_ps
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


(Otiai10) #3
docker system prune -a

wish it works


(Ramizrshaik) #4

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?


(Im7mortal) #5

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

LABEL HASH_COMMIT=${HASH_COMMIT}
# instead of 
LABEL "HASH_COMMIT"=${HASH_COMMIT}

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:


(Im7mortal) #6

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