Cleaning local docker cache

1 minute read

I had encountered at least two situations when I had to clean up the entire local Docker cache. One is when it weighs around 30 gigabytes, and the second one was when something was so broken that I had to delete every local cache file. When te first case might be typical, I don't wish anyone to have to deal with the second one. Without further ado: How to clean local Docker files?

As per the official Docker documentation, we have a set of commands that can help clean up our local setup at our disposal. I pick a few to achieve my goal. First of all, it will be great to see what is already running on my local docker.

docker ps

Then I can list all of the Docker containers.

docker ps -a

To delete everything, I need to stop every running container.

docker stop $(docker ps -a -q)

And then I can remove every container in single line.

docker rm $(docker ps -a -q)

When containers are deleted, we can move to remove images and networks. Usually, there is no need to do that, but if after removing all containers, something is still broken, that can be our last chance.

docker system prune -af

As per the Docker manual, this command will remove unused data. Instead of deleting only unused data, we want to remove everything. That's what "a" flag is doing. Flag "f" is to skip confirmation prompt. This operation is destructive. Be mindful when using it.

Just in case I do have an alias in my zsh config. It may be overkill since I'm not using these commands very often. However, I'm not too good at remembering commands.

function docker_reset {
  docker stop $(docker ps -a -q)
  docker rm $(docker ps -a -q)
  docker system prune -af
}

I'm going to create some content about creating stuff rather than destroying it at some point.



Tags: docker, scripts

← Back home