DEV Community

Ptagl
Ptagl

Posted on • Originally published at ptagl.github.io on

Claiming WSL Disk Space

TLDR

Shut down your WSL distributions and use PowerShell (as administrator) to run Optimize-VHD on the VHDX file used by your WSL distribution:

wsl --shutdown
Optimize-VHD -Path '<Path to your VHDX file>' -Mode Full
Enter fullscreen mode Exit fullscreen mode

Introduction

Working with WSL I sometimes see my disk usage grow significantly until the day I'm almost out of space and need to reclaim it somehow. In my case, the reason is that WSL uses a virtual hard disk (VHDX) file to store the file system of each distribution, and over time these files grow in size as data is added, but they don't shrink when data is removed.

Note: this is true unless your WSL distribution is configured to use the "sparse mode", which should automatically resize the VHDX file as needed. However, many people reported issues with this mode (for instance here, here, and here), so it's not considered a reliable solution as of today.

It’s easy to check whether you have this issue too. Since I always forget the path of my WSL VHDX file, I typically use PowerShell to find it:

(Get-ChildItem -Path $env:USERPROFILE\AppData\Local\Packages\ -Filter "*.vhdx" -Recurse -ErrorAction SilentlyContinue).FullName
Enter fullscreen mode Exit fullscreen mode

Copy the path and store it in a variable for convenience:

$VHDXPath = '<Path to your VHDX file>'
Enter fullscreen mode Exit fullscreen mode

Then, check the size of the VHDX file:

"{0:N2} GB" -f ((Get-Item $VHDXPath).Length / 1GB)
Enter fullscreen mode Exit fullscreen mode

Now, it's time to compare this size with the actual used space inside the WSL distribution. To do this, start your WSL distribution and run:

df -h /
Enter fullscreen mode Exit fullscreen mode

If the size of the VHDX file is significantly larger than the used space reported by df, then it's definitely time to reclaim some bytes!

Optimize-VHD to the rescue

Luckily, there is a PowerShell cmdlet called Optimize-VHD that can be used to compact VHDX files. This command reclaims unused space in the VHDX file, effectively reducing its size on disk.

Note: Optimize-VHD is part of the Hyper-V module. Availability on your system depends on your Windows edition and features installed. If you don't have it, you might need to enable the Hyper-V feature on your Windows system via "Turn Windows features on or off" in the Control Panel.

First of all, make sure the VHDX file is not mounted (the easiest way is to stop WSL), then run the Optimize-VHD command as administrator on the VHDX file you found earlier:

wsl --shutdown
Optimize-VHD -Path $VHDXPath -Mode Full
Enter fullscreen mode Exit fullscreen mode

The process may take some time depending on the size of the VHDX file and the amount of unused space to reclaim.

After the command completes, you can check the size of the VHDX file again with the same commands used above to see how much space you've reclaimed.

And that's all, see you next time your disk is full!

Top comments (1)

Collapse
 
ian_packard_2d172794449d1 profile image
Ian Packard • Edited

Great post! One tip that can significantly improve your compaction results: run fstrim -av inside the distro before running Optimize-VHD. This marks deleted blocks as zeros, which the compaction process can then reclaim - without it, Optimize-VHD can only work with blocks that were already zeroed, leaving a lot of reclaimable space on the table.

I built WSL UI - partly to automate this - it handles the fstrim + shutdown + optimize sequence in one click if you want to skip the manual steps.