How To: Shrink WSL Disk Size on Windows 10
Sep 25, 2023
I recently downloaded and processed some large files on Windows Subsystem for Linux (WSL) on Windows 10. Once I was done with the files, I deleted them from my Ubuntu installation, but the VHD file was still taking up the same amount of space as it was before I deleted the files.
It turns out that Windows and WSL does not automatically shrink the VHD file if you delete files - it just automatically expands it as you use more space in WSL.
How to shrink the VHD file
Locate the VHD file in your user directory of Windows. For me, this was: “C:\Users\Nick Plunkett\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx”
In order to reclaim the disk space that is no longer in use in WSL, open a powershell window and run the following commands:
Identify the correct WSL instance:
PS C:\Users\Nick Plunkett> wsl.exe --list --verbose NAME STATE VERSION * Ubuntu Running 2
Make sure it is shutdown before performing disk operations:
wsl.exe --terminate Ubuntu
Select the vdisk file
select vdisk file="C:\Users\Nick Plunkett\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx"
Run “compact vdisk”. This will take some time depending on the difference between the size of the VHD file and the free space in that VHD file.
For me, this saved over 200GB of unused but allocated space. As I added more files to WSL, the VHD file grew in size again.
Thanks to Stephen Rees-Carter, I found this process on their website originally at https://stephenreescarter.net/how-to-shrink-a-wsl2-virtual-disk/