Back to blog
Nov 13, 2024
4 min read

Freeing Up Disk Space in WSL after Deleting Large Files through Windows Explorer

A comprehensive guide on reclaiming disk space in Windows Subsystem for Linux (WSL) when deleted files don't reduce the VHDX size.

Recently, I ran into an unexpected issue while working in my Ubuntu WSL instance. I had downloaded around 200GB of data, which I later decided to delete using Windows File Explorer. But even after deleting everything, my disk space hadn’t freed up – Windows still showed my Ubuntu instance as taking up 200GB of space.

After some digging, I figured out why this happens and how to actually free up the space.

The Problem

In WSL, all data is stored in a virtual hard disk file, usually named ext4.vhdx, located on your Windows file system. This file automatically grows as you add data but doesn’t shrink back down when you delete data using Windows File Explorer. So, while my Ubuntu instance showed that the files were gone, the ext4.vhdx file still occupied 200GB on my disk.

Solution: Export and Re-import WSL

The best way to handle this issue in WSL is to export the WSL instance to a .tar file and then re-import it. This process reclaims the space taken up by deleted files and compacts the .vhdx file to its minimum possible size. Here’s how to do it step-by-step.

Step 1: Check Disk Usage

First, I checked my Ubuntu WSL instance to see how much actual data was left after deleting the files. This helped ensure that I had enough space to export the instance.

In the WSL terminal, I ran:

df -h

This command showed me the disk usage in my Ubuntu instance, and I confirmed that the actual data size was around 20GB instead of the original 220GB.

Step 2: Export the WSL Instance

The wsl —export command lets you create a .tar backup of your WSL instance, containing only the existing data, which is what we need to optimize the disk space usage.

In PowerShell, I ran:

wsl --export Ubuntu ubuntu.tar

This command created a ubuntu.tar file with just the actual data left in my WSL instance, excluding the previously deleted files. Since my remaining data was only around 20GB, the .tar file was around the same size.

Step 3: Unregister the Current Instance

To reclaim space, we need to delete the current WSL instance, including its large .vhdx file. Don’t worry—the .tar file we just created is a full backup of the instance.

In PowerShell, I ran:

wsl --unregister Ubuntu

This command unregistered the Ubuntu instance, which also deleted the large .vhdx file from my system, freeing up the disk space.

Step 4: Re-import the WSL Instance

Now that the old, bloated .vhdx file was gone, I re-imported the Ubuntu instance from the .tar file. I used the default WSL location to ensure it was restored correctly.

wsl --import Ubuntu "C:\Users\<YourUsername>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState" ubuntu.tar

After running this command, my Ubuntu instance was back, registered, and taking up only the actual disk space needed for the data it contained (around 20GB).

Good to know: To make sure, check in C:\Users<YourUsername>\AppData\Local\Packages\ and find your specific WSL package folder. In my case it was CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc

Step 5: Confirming the Space Was Reclaimed

To verify that the process worked and the space was reclaimed, I checked the WSL disk usage in the Windows Settings. The total size of my Ubuntu instance had indeed reduced significantly.

Additional Notes If you run into issues with this method, make sure your WSL distribution is not currently running by using:

wsl --shutdown

Conclusion

This process is a useful tool for anyone working with large data sets in WSL or frequently downloading and deleting large files. If you’re ever in a similar situation, I hope this guide helps you reclaim your storage!