Dev note: 20230713 How to release WSL disk space
Question
- Ubuntu running under Windows Subsystem for Linux (WSL) popped up error about inefficient memory in virtual disk
- I have to expand the memory of virtual disk
Source: How to manage WSL disk space | Microsoft Learn
Procedure
-
Open Windows Command Prompt with admin privileges and then open the diskpart command
diskpart -
Wait for diskpart awake, using
Select vdiskto specify the path to disk, but most of the time, the path to VHD (Virtual Hard Disk) is unknown, so I have to use other prompt to find it.- I found the resolution as following source: How to manage WSL disk space | Microsoft Learn
(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq 'distribution-name'- However, what is
distribution name? -> preciously, it means the name of distro of Linux OS under your subsystem, such as Ubuntu, Arch Linux, et al. So I use following code to find out the name of my distro:
wsl -l -vOutputs are following:
NAME STATE VERSION * Ubuntu-22.04 Stopped 2- It is
Ubuntu-22.04. After I replaced it into'distribution-name', got the path.
(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq 'Ubuntu-22.04'Outputs are following:
C:\Users\LI\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx- Finally, I could use the prompt to select the virtual disk as:
Select vdisk file = "C:\Users\LI\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx" -
Check the detail of virtual disk with prompt:
detail vdiskGot infos as outputs:
Vendor ID: {00000000-0000-0000-0000-000000000000} (Unknown) State: Added Virtual size: 256 GB Physical size: 240 GB Filename: C:\Users\LI\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx Is Child: No Parent Filename: Associated disk#: Not found.→ Well, 256 GB is not enough, I have already used 95% of them.
-
Expand the size of the virtual disk and exit with the following prompts:
Notes: the units of the disk size is MB, so I enlarged the size of virtual disk 100% of the originalexpand vdisk maximum=512000exit -
Not the end. I have to let the subsystem recognize the renewed disk size, after opened the wsl and activate Ubuntu, prompt:
sudo mount -t devtmpfs none /dev mount | grep ext4and, with
resize2fs(specify the correct path:/dev/sdcas my case, and don't forget the unit symbol M)sudo resize2fs /dev/sdc 512000M -
Check if it works, with
df:

⇾ Congratulation.
#### SOLVED ####