Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Docker images and containers are deleted, but the Windows disk is still full

Errordocker rm and docker rmi finish, but the C: drive stays full

Short answer

Two separate things hold space. Docker itself keeps volumes, tagged images and build cache that a plain prune leaves alone. And on Windows the data lives in a virtual disk file that grows as you use it but does not shrink by itself. Prune what Docker keeps, then compact the .vhdx with Windows tools.

  • Applies to Docker Desktop 4.x on Windows 11, WSL 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask Docker how much space it believes it uses, split by type. If the numbers are small, Docker is clean and the space is held by the virtual disk file.

    docker system df
  2. 2Match what you see, then jump to its fix

    1. 1docker system df shows large reclaimable numbers for images, local volumes or build cache.Volumes, tagged images and build cache were not prunedFix
    2. 2docker system df is small, yet the .vhdx file on Windows is many gigabytes.The WSL virtual disk grows but does not shrink by itselfFix
    3. 3You compact the disk regularly and it fills up again.New virtual disks are not sparseFix

What does the error look like?

  • docker rm and docker rmi finish, but the C: drive stays full
  • docker system df shows a large RECLAIMABLE size

Cause 1 of 3

Volumes, tagged images and build cache were not pruned

1Check

By default docker system prune removes stopped containers, unused networks, dangling images and unused build cache. It leaves volumes alone and keeps tagged images that are not dangling.

docker system df -v

2Fix

Add --all to remove all unused images, and --volumes only when you are sure no volume holds data you need.

docker system prune --all
docker system prune --all --volumes

3Verify

Run docker system df again; the reclaimable sizes drop.

docker system df

Confirmed by docs.docker.com

Did this fix it?

Cause 2 of 3

The WSL virtual disk grows but does not shrink by itself

1Check

WSL 2 stores its distributions in virtual hard disk files that resize automatically to meet storage needs. Deleting data inside does not by itself return the space to Windows. The compact step does: Optimize-VHD reclaims unused blocks, and needs the disk not attached or attached read-only.

wsl -l -v
(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq '<distribution-name>' }).GetValue("BasePath")

2Fix

Quit Docker Desktop, shut WSL down, then compact the disk file in an elevated PowerShell. Optimize-VHD comes with the Hyper-V PowerShell module.

wsl --shutdown
Optimize-VHD -Path "<path-to-the-vhdx-file>" -Mode Full

3Verify

Check the size of the .vhdx file in Explorer or with Get-Item before and after.

(Get-Item "<path-to-the-vhdx-file>").Length / 1GB

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

New virtual disks are not sparse

1Check

In .wslconfig, the experimental sparseVhd setting makes any newly created VHD sparse automatically. It is off by default and applies only to disks created after you turn it on.

type $env:USERPROFILE\.wslconfig

2Fix

Enable the setting and restart WSL. Existing disks are not converted by it.

# %UserProfile%\.wslconfig
[experimental]
sparseVhd=true

3Verify

Restart WSL, and check the setting is in the file.

wsl --shutdown

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Run docker system df now and then, and prune build cache on a schedule.
  • Keep large data in named volumes that you remove on purpose, not in the container's writable layer.
  • Take a copy of any volume you need before running prune with --volumes.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.