How to reclaim the space used by WSL2's ext4.vhdx

2 min read 25-09-2024
How to reclaim the space used by WSL2's ext4.vhdx

Reclaiming Disk Space Used by WSL2's ext4.vhdx

If you're using Windows Subsystem for Linux 2 (WSL2), you may have noticed that it can take up a significant amount of disk space. This is because WSL2 uses a virtual hard disk (VHDX) file, named ext4.vhdx, to store the Linux file system. Over time, this file can grow in size as you install applications and create data.

Here's an example:

C:\Users\<your_username>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx 

While this is a common occurrence, you can reclaim this space without losing any data. Here's a breakdown of how:

Understanding the Problem

The ext4.vhdx file can become large for two primary reasons:

  1. Unused Space: As you install and uninstall applications, delete files, and even when simply updating your distribution, WSL2 doesn't immediately shrink the ext4.vhdx file to reclaim the freed space. It keeps it reserved, potentially leading to wasted disk space.
  2. Data Growth: Your Linux distribution naturally grows as you install new applications and create data.

Reclaiming Disk Space

There are two main methods for reclaiming space used by ext4.vhdx:

1. Using Disk Cleanup:

  • Open the Windows search bar and type "Disk Cleanup."
  • Select "Disk Cleanup" from the results.
  • Choose the drive containing your WSL2 installation (usually your C: drive).
  • Click "Clean up system files" to include temporary files and other system files.
  • Select the checkbox for "Previous Windows installations" and any other files you want to delete.
  • Click "OK" to start the cleanup process.

2. Shrinking the VHDX File:

  • Important: This method is more advanced and requires some command-line knowledge. It is recommended to create a backup of your ext4.vhdx file before proceeding.

  • Step 1: Stop all WSL instances. You can do this by closing all open terminal windows.

  • Step 2: Open PowerShell as administrator.

  • Step 3: Execute the following command, replacing <your_username> with your actual username:

    powershell -Command "Mount-VHD -Path 'C:\Users\<your_username>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx' -Passthru | Shrink-VHD -Path 'C:\Users\<your_username>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx' -Size 128GB"
    
  • Step 4: Adjust the -Size parameter to your desired size. Be sure to leave enough space for your Linux distribution to grow comfortably.

  • Step 5: Unmount the VHDX file:

    Dismount-VHD -Path 'C:\Users\<your_username>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx' 
    

Additional Tips

  • Regularly clean up your Linux distribution: Use tools like sudo apt autoremove to remove unneeded packages and free up space.
  • Check for large files: Use the du command to identify large files or directories in your Linux distribution that might be contributing to the disk space usage.
  • Consider using a smaller Linux distribution: If you don't need all the features of a full-fledged distribution, a smaller one like Alpine Linux might be a better option.

Resources

By following these steps and adopting good file management practices, you can effectively manage the disk space used by WSL2 and prevent it from becoming a burden on your system.