Dev note: 20230713 How to release WSL disk space

Question

Source: How to manage WSL disk space | Microsoft Learn

Procedure

  1. Open Windows Command Prompt with admin privileges and then open the diskpart command

    diskpart
    
  2. Wait for diskpart awake, using Select vdisk to 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.

    (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 -v
    

    Outputs 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"
    
  3. Check the detail of virtual disk with prompt:

    detail vdisk
    

    Got 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.

  4. 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 original

    expand vdisk maximum=512000
    
    exit
    
  5. 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 ext4
    

    and, with resize2fs (specify the correct path: /dev/sdc as my case, and don't forget the unit symbol M)

    sudo resize2fs /dev/sdc 512000M
    
  6. Check if it works, with df:
    Pasted image 20230713142250.png

⇾ Congratulation.

#### SOLVED ####