Member-only story
Reclaiming Disk Space from WSL2: A Personal Experience

As an avid user of the Windows Subsystem for Linux (WSL2), I often find myself working with large files and datasets within my Ubuntu environment. However, after deleting some of these files, I noticed that the disk space wasn’t being released, which was frustrating. Fortunately, I discovered a solution to reclaim the disk space from my WSL2 virtual disk, and I’m excited to share my experience with you.
The Problem
After working on a project that involved processing and analyzing large datasets, I ended up with several gigabytes of files stored within my WSL2 instance. Once the project was completed, I deleted these files to free up space. However, to my surprise, the disk space wasn’t being released, even after restarting WSL2.
This issue can occur because WSL2 uses a virtual hard disk (VHDX) file to store its file system. When you delete files within WSL2, the space is marked as available within the file system, but the VHDX file itself doesn’t shrink automatically. This means that the disk space isn’t actually reclaimed on your host machine.
The Solution: Compacting the VHDX File
After some research, I stumbled upon a solution that involved using the “diskpart” tool to compact the VHDX file. This process allows you…