16

Following the advice on How do I get back unused disk space from Ubuntu on WSL2? I set my WSL disk image to sparse with wsl --manage <distro> --set-sparse true. But the ext4.vhd file is still very large (~70 GB) while running du inside WSL reports only about 10 GB.

Will the disk size be reduced dynamically as I use my C:/ drive or do I have to manually reduce it? If I can, I'd like to force update the now sparse disk so that I know how much free space I actually have left on my drive.

name.disp
  • 263

3 Answers3

16

You can revert the sparse mode, run the optimize-vhd command, and then set it back to sparse. something along this lines. not sure what's the implication of this, at least it works in my case.

  1. Revert sparse mode

wsl --manage <distro> --set-sparse false

  1. Optimize the disk

optimize-vhd -Path <path-to-your-vhdx>.vhdx -Mode full

  1. Set it back to sparse mode

wsl --manage <distro> --set-sparse true

9

For those who don't have access to Optimize-VHD applet in Powershell due to being in Windows Home, you can do the following in CMD:

wsl --manage <distro> -s false

diskpart

select vdisk file="<distro-location>.vhdx" attach vdisk readonly compact vdisk detach vdisk

exit

; EQUIVALENT TO ; Optimize-VHD -Path "<distro-location>.vhdx" -Mode Full -Verbose

wsl --manage <distro> -s true

0
  1. Where did you get the data of 70GB? If it’s the file size displayed by Windows, then this data is likely to be unreal;
  2. The real size should be its occupied space, you should check its occupied space in the file properties instead of size (Windows explorer only displays file size on the surface, occupied space will only be displayed in properties);
  3. After setting to sparse using wsl --manage <distro> -s true. Although the file size displayed by Windows explorer does not decrease with the deletion of files, its actual size (occupied space) is indeed decreasing.
Wit-l
  • 17