How to Resize WSL2 VHDX Virtual Disk

I was struggling to compact my WSL VHDX disk, it has hardly 10 GB data but it was showing 72 GB in size. 


I used following method one which gave 1 GB benefit. But method 2 saved 50GB+ space. (Last screenshot in this article)


Method 1: (Not working)

In command Prompt

diskpart

DISKPART> select vdisk

file="C:\Users\yourusername\AppData\Local\Packages\WhitewaterFoundryLtd.Co.16571368D6CFF_kd...\LocalState\ext4.vhdx"

DISKPART> compact vdisk


Same instructions available here 

Method 2:  Using WSLCOMPACT

Open Powershell ISE


Download the github repo https://github.com/okibcn/wslcompact

Open the files inside WslCompact folder in powershell



Select all the code in this file WslCompact.psd1 and click RUN SELECTION (as shown in below image)


This would allow you to run the wslcompact method.


Then you need to set a temp path, i used my D Drive.

$env:TEMP="D:\tmp_c"

Then run the following

WslCompact -c Debian 

WslCompact -c Ubuntu

Depends on your linux distro



You may see an error like below if wsl is still not shutdown properly.


If the operation is successful you would see


 New Image compacted from 73082 MB to 22884 MB

 Do you want to apply changes and use the new image (y/N): y

 Image replaced for distro: Debian

Done.

11 Comments

Previous Post Next Post