If you’ve used Docker Desktop on Windows for a while, you’ve probably hit the annoying problem when your free disk space slowly disappears. Even when you delete containers or images, the space never comes back. Docker just keeps growing until your drive is full.

Why Docker Consumes So Much Space on Windows

Docker Desktop runs inside a WSL2 virtual machine. All Docker data (like images, layers, containers, volumes) lives inside a single virtual disk file (VHDX). Depending on your Docker version, it’s stored in one of these locations:

%LOCALAPPDATA%\Docker\wsl\data\ext4.vhdx          (older installs) 
%LOCALAPPDATA%\Docker\wsl\disk\docker_data.vhdx   (newer installs)

This VHDX grows as Docker pulls images or writes data. And it never shrinks automatically, even if you delete everything inside Docker, the VHDX stays huge until you manually compact it.

To fix this, you need to:

  1. Clean up Docker’s internal data
  2. Compact the underlying VHDX file

Configure WSL Disk Behavior

You can control how much memory, CPU, VHDX size, etc., using a %UserProfile%\.wslconfig file. Or, if you prefer the GUI, Windows 11 has a WSL Settings tool that can be found in the Start menu.

Example of .wslconfig file:

[wsl2]
processors=4
memory=4294967296
defaultVhdSize=32463912960

This doesn’t shrink the disk by itself, but it keeps things predictable.

Clean Up Docker Data

Before shrinking the VHDX, remove unused Docker data (images, containers, volumes):

docker system prune -a --volumes

This clears:

  • stopped containers
  • dangling layers
  • unused images
  • unused volumes

Compact the Docker VHDX File

Once Docker is cleaned up, shrink the actual VHDX file. Open the terminal in the administrator mode:

  • Shut down WSL:
wsl --shutdown
  • Compact the Docker VHDX:
Optimize-VHD -Path %LOCALAPPDATA%\Docker\wsl\disk\docker_data.vhdx -Mode Full

This forces Windows to reclaim unused blocks and physically shrink the file. After this step, the VHDX size should drop dramatically, often from tens of gigabytes down to just a few.

Note: You need the Hyper-V module installed for Optimize-VHD.

Script

Here’s a PowerShell script to automate the process:

Write-Host "[INFO] Cleaning up Docker data..." -ForegroundColor Cyan
docker system prune -a --volumes -f

Write-Host "[INFO] Shutting down WSL..." -ForegroundColor Cyan
wsl --shutdown

# Known Docker VHDX locations
$paths = @(
    "$env:LOCALAPPDATA\Docker\wsl\data\ext4.vhdx",
    "$env:LOCALAPPDATA\Docker\wsl\disk\docker_data.vhdx"
)

# Find all existing VHDX files
$foundVhds = $paths | Where-Object { Test-Path $_ }

if (-not $foundVhds) {
    Write-Host "[WARN] No Docker VHDX files found in known locations." -ForegroundColor Yellow
    return
}

# Check for Hyper-V module
if (-not (Get-Module -ListAvailable -Name Hyper-V)) {
    Write-Host "[WARN] Hyper-V module not found. Required for Optimize-VHD." -ForegroundColor Yellow
    $answer = Read-Host "Install Hyper-V now? (y/n)"

    if ($answer -ne "y") {
        Write-Host "[ERROR] Hyper-V not installed. Cannot compact VHD files. Exiting." -ForegroundColor Red
        return
    }

    Write-Host "[INFO] Installing Hyper-V..." -ForegroundColor Cyan
    try {
        Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -ErrorAction Stop
        Write-Host "[OK] Hyper-V installed." -ForegroundColor Green
    } catch {
        Write-Host "[ERROR] Failed to install Hyper-V. Cannot continue." -ForegroundColor Red
        return
    }
}

foreach ($vhd in $foundVhds) {
    Write-Host "[INFO] Compacting VHDX: $vhd" -ForegroundColor Cyan
    try {
        Optimize-VHD -Path $vhd -Mode Full
        Write-Host "[OK] Successfully compacted: $vhd" -ForegroundColor Green
    } catch {
        Write-Host "[ERROR] Failed to compact: $vhd" -ForegroundColor Red
    }
}

Write-Host "[OK] Done. Disk space reclaimed." -ForegroundColor Green