The Windows utility diskpart
can now be used to shrink Virtual Hard Disk (vhdx) files that is used to store Windows Subsystem for Linux (WSL).
I am putting the list of the instructions below.
First make sure all WSL instances are shut down by opening an administrator command window, and typing:
1 2 |
wsl --shutdown |
Verify everything is stopped by:
1 2 |
wsl.exe --list --verbose |
Then start diskpart
:
1 2 |
diskpart |
and inside diskpart
type:
1 2 |
DISKPART> select vdisk file="<path to vhdx file>" |
For example:
1 2 |
DISKPART> select vdisk file="C:\Users\user\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_12rqwer1sdgsda\LocalState\ext4.vhdx" |
it should respond by saying DiskPart
successfully selected the virtual disk file.
Then to shrink
1 2 |
DISKPART> compact vdisk |
After this the vhdx
file should shrink in usage. In my case it went from 40GB to 4GB. You can type exit
to quit diskpart.