Regain disk space occupied by Docker

If you are irritated with Docker WindowFilter taking up lot of your disk space, then don’t worry, You are not the only one. The accumulation of files under Docker > WindowFilter is a common problem.

Recently, I was running out of disk space and was looking of ways to regain that.

Most of googled options would ask you to docker system prune which removes all dangling images, containers and volumes. It freed up some space but still there was scope.

I used TreeSize software to check folder which are largely occupying space. And culprit came out to be WindowsFilter folder. Over time, Docker container create layers of unused data in WindowsFilter folder eating up disk space. It can be frustrating when it occupies significant space (~140 GB in my case).

Although Docker has in-built ability to identify & clean these orphan layers which occurs when docker shuts down. But by default it gets only 15 seconds to do so which is not enough to handle large files. And it results into piling up orphan layers.

But deleting it is not that simple –

  • First, it won’t let you delete it with Window’s Delete option
  • Second, deleting all folders can delete your containers if those folders are in use.

Thinking of Options…

One suggestion was  docker-ci-zap (Read More) But I was reluctant to do as few cautioned to use it at your own risk as it may disturb your Docker Service which is not ideal. So I skipped it.

NOTE: If you wish to forcefully delete the windowsfilter folder, then it is a great (rather, the best) option. This works even on orphaned windowsfilter folders when you don’t have the Docker service running. Simply download zip from here (GIT repo – https://github.com/moby/docker-ci-zap ) and run file as mentioned in Readme with appropriate parameters/folder path.

My Solution

Fortunately I landed on one of the discussion which has a PowerShell script to help us vacate space.

Steps to perform

  • Download PowerShell script and execute (as Administrator) with parameter  -RenameOrphanLayers
.\Find-OrphanDockerLayers.ps1 -RenameOrphanLayers

This will add -removing suffix to all orphan layer lying in the folder as below. This won’t actually remove it, you need further steps to do so.

Now the cleanup occurs when docker service shuts down. But Default shutdown timeout is only 15 seconds which is not enough to clean up these layers. So you need to allow more time for this activity. (I needed around 25-30 minutes to clean all data.) But how to do that ?

It easy, just

  • Stop the Docker service,
  • Start it manually with command
dockerd -D --shutdown-timeout 900 

This 900 here, are seconds which is 15 minutes. You can set it as per your need.

It will halt at below line, just press + C and be patient.  It will show you logs of cleanup,

time="2023-10-04T20:17:19.029790000+05:30" level=info msg="API listen on //./pipe/docker_engine"

Now orphan layers will be removed one by one and you will start seeing similar message as below –

time="2023-10-04T20:18:52.453215900+05:30" level=info msg="Cleaned up 166c1633f5ed627a97895aaf32833ef931a225e8550468c9013dc3d678658b08-removing"

At the end, if it is able to Clean all orphan layers, then you will see below message –

time="2023-10-05T12:12:36.181982100+05:30" level=debug msg="Clean shutdown succeeded" 
time="2023-10-05T12:12:36.193753900+05:30" level=info msg="Daemon shutdown complete"

In case this time not enough to clean those files, it will forcefully shutdown with below message –

time="2023-10-04T20:33:46.172659200+05:30" level=error msg="Force shutdown daemon"
time="2023-10-04T20:33:46.176820100+05:30" level=info msg="Daemon shutdown complete"

If this happens, execute below command again to allow it more time for clean-up.

dockerd -D --shutdown-timeout 900 

I hope this gives you enough SPACE 😉

Leave a comment