Docker Desktop WSL ext4.vhdx too large
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The ext4.vhdx file is the virtual hard disk that WSL 2 uses to store the Linux filesystem for Docker Desktop. It grows automatically as you pull images and create containers, but it never shrinks on its own when you delete data. To reclaim space, you need to first clean up Docker resources with docker system prune, then compact the VHDX file using either Optimize-VHD (Hyper-V) or wsl --manage --resize (Windows 11+). Without the compaction step, the file stays at its peak size regardless of how much data you delete.
Why the File Grows But Never Shrinks
WSL 2 uses a dynamically expanding virtual hard disk in VHD format. The file starts small and grows as data is written to the Linux filesystem. However, the VHD format does not automatically reclaim space when files are deleted inside the virtual disk. The filesystem marks blocks as free internally, but the host NTFS filesystem still sees those blocks as allocated in the VHDX file.
This behavior means that a developer who pulls 50 GB of Docker images, deletes them all, and runs docker system prune will still have a 50 GB ext4.vhdx file on disk.
Where to Find the File
The default locations for the Docker Desktop VHDX file:
You may also find VHDX files for other WSL distributions at:
Step 1: Clean Up Docker Resources
Before compacting the virtual disk, remove everything Docker no longer needs. This frees space inside the virtual filesystem so the compaction step has something to reclaim.
Understanding docker system df Output
The "RECLAIMABLE" column shows how much space docker system prune can recover. In this example, pruning would free roughly 22 GB inside the virtual disk.
Step 2: Compact the VHDX File
After cleaning up Docker resources, the space is free inside the Linux filesystem but the VHDX file on the Windows host is still the same size. You need to compact it.
Method 1: Optimize-VHD (Requires Hyper-V)
This method works on Windows 10/11 Pro, Enterprise, or Education with Hyper-V enabled.
Method 2: diskpart (Works on Windows Home)
For Windows Home editions that do not have Hyper-V:
Method 3: wsl --manage (Windows 11 22H2+)
Recent Windows 11 builds support resizing directly through the WSL CLI:
The --set-sparse flag is particularly useful because it enables automatic space reclamation on future deletes, solving the root cause rather than treating the symptom.
Preventing Future Growth
Set Docker Resource Limits
In Docker Desktop settings, you can limit the virtual disk size:
- Open Docker Desktop Settings
- Go to Resources then Advanced
- Set "Disk image max size" to a reasonable limit (e.g., 64 GB)
Use .wslconfig to Limit WSL Resources
Implement Regular Cleanup
Create a scheduled cleanup script:
Use Multi-Stage Builds
Large images are often the primary cause of VHDX bloat. Multi-stage builds keep the final image small:
Comparison of Compaction Methods
| Method | Windows Edition | Requires Admin | Automatic Reclaim | Notes |
Optimize-VHD | Pro/Enterprise/Education | Yes | No | Most reliable, requires Hyper-V module |
diskpart compact | All editions including Home | Yes | No | Works everywhere, slightly slower |
wsl --manage --set-sparse | 11 22H2+ | No | Yes | Best long-term fix, prevents regrowth |
wsl --manage --resize | 11 22H2+ | No | No | Sets a hard cap on disk size |
Common Pitfalls
- Running compaction without cleaning Docker first. If you compact the VHDX while Docker images are still present, you reclaim nothing. Always
docker system prune -a --volumesbefore compacting. - Forgetting to shut down WSL before compacting. The VHDX file is locked while WSL is running.
Optimize-VHDanddiskpartwill fail or produce corrupt results if WSL is still active. Always runwsl --shutdownfirst and wait a few seconds. - Using
docker system prunewithout the-aflag. Without-a, Docker only removes dangling images (untagged intermediate layers). With-a, it removes all images not used by a running container. The difference can be tens of gigabytes. - Not realizing volumes survive
docker system prune. You need the--volumesflag to prune named volumes. Without it, persistent data volumes remain and continue consuming space. - Trying
Optimize-VHDon Windows Home. The Hyper-V PowerShell module is not available on Home editions. Use thediskpartmethod instead. - Compacting too infrequently. If you wait until the VHDX file consumes your entire disk, the compaction process itself may fail due to insufficient temporary space. Schedule monthly compaction.
Summary
The ext4.vhdx file grows as Docker stores images, containers, and volumes in the WSL 2 virtual disk, but it never shrinks automatically. Reclaiming space is a two-step process: first clean Docker resources with docker system prune -a --volumes, then compact the VHDX file using Optimize-VHD (Hyper-V), diskpart compact vdisk (Windows Home), or wsl --manage --set-sparse true (Windows 11 22H2+ for automatic future reclamation). For long-term prevention, limit Docker's disk image size in settings, use multi-stage builds to keep images small, and schedule regular cleanup scripts. The sparse file option in recent Windows 11 builds is the most effective solution because it addresses the root cause by enabling automatic space reclamation.

