Posts Fixing WSL Disk Space Issue by Compacting VHD
Post
Cancel

Fixing WSL Disk Space Issue by Compacting VHD

Fixing WSL Disk Space Issue by Compacting VHD

Windows Subsystem for Linux (WSL) allows you to run a full Linux distribution within Windows. However, if your WSL is using too much disk space, even after deleting files, it’s possible that the space is not being released automatically due to how WSL stores its data in a virtual hard disk (VHD) file.

In this tutorial, we’ll guide you through the process of compacting the VHD file to free up disk space manually.

Context

When using WSL 2, your Linux file system is stored inside a .vhdx file, which behaves like a virtual hard disk. Even when files are deleted inside your Linux distribution, the .vhdx file doesn’t automatically shrink. Over time, this can lead to disk space issues on your Windows system, especially if you frequently manipulate large files in WSL.

To solve this, we can manually compact the .vhdx file to free up unused space.

Step-by-Step Solution

Follow these steps to fix the WSL disk space issue:

1. Shut Down WSL

First, make sure no WSL distributions are running by executing the following command in PowerShell:

1
wsl --shutdown

This ensures that the WSL instance is fully stopped and the .vhdx file can be safely accessed.

2. Locate the VHDX File

To locate the .vhdx file for your WSL distribution, use this PowerShell command. Replace <distribution-name> with your actual WSL distribution name, like Ubuntu-24.04:

1
(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq '<distribution-name>' }).GetValue("BasePath") + "\ext4.vhdx"

For example, if you’re using Ubuntu-24.04, the result would be something like:

1
C:\Users\YourUser\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu24.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx

3. Compact the VHDX File

Now that you have the path to the .vhdx file, you can compact it using DiskPart. Follow these steps:

  1. Open PowerShell as Administrator.
  2. Run the diskpart command to enter the DiskPart utility.
  3. Select the virtual disk file using this command:

    1
    
     select vdisk file="C:\Users\YourUser\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu24.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx"
    
  4. Mount the disk as read-only:

    1
    
     attach vdisk readonly
    
  5. Now, compact the disk:

    1
    
     compact vdisk
    
  6. Finally, detach the virtual disk:

    1
    
     detach vdisk
    

4. Restart WSL

After successfully compacting the .vhdx file, restart WSL:

1
wsl

This will ensure that WSL starts with the newly compacted disk and frees up the space on your Windows system.

Conclusion

By following these steps, you can manually reclaim disk space used by WSL without having to reinstall or reset your distribution. This is a helpful method for maintaining your Windows system’s health when running large workloads inside WSL.


References:

This post is licensed under CC BY 4.0 by the author.