How to delete cache?

Is there a way to delete the Docker cache?

2 Likes

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

3 Likes
docker system prune -a

wish it works

10 Likes

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

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:

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

16 Likes

@harendra122
This did the trick for me.

Thankyou

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”?
Thanks!

Works great, thanks!

It works great, thank you very much

The solution suggested by harendra122, of using docker builder prune seems to have worked for me. More details here: docker pull always show "Already exists" for layers during pull even after deleting all images - Stack Overflow

Following article explains it in details

How to clear docker cache

“docker builder prune” does the trick!

Thank you for saving some of my hair!

I used “docker buildx prune -f” to clear the cache only
you can also use “docker systemc df” to check space for Images, Containers, Local Volumes, and Build Cache

Doesn’t work for me on Debian:

docker systemc df

docker: ‘systemc’ is not a docker command.
See ‘docker --help’

Update: it’s docker system df

Yes thank you for right solution.

bro lots of thank you. you saved my job :rofl:

1 Like

Open the command prompt and enter the command docker system prune and then press enter and after this press y for confirmation

I’ve got another way :wink:

To delete the docker build cache, you can use this command [mod update: remove spam link]:

docker builder prune

This command will prompt you to confirm the deletion of the cache. If you wanna delete the cache without any prompts, you can use:

docker builder prune -f

For more options and details, check the docker documentation on builder prune.