WSL 2 makes it easy to forget that your Linux filesystem is backed by a virtual disk file on Windows.

That is usually fine until the disk keeps growing and you realize deleting files inside Linux does not automatically shrink the .vhdx on the Windows side.

The Goal

The goal here was to compact the backing disk for a WSL 2 distribution after cleanup.

1. Confirm the Distribution

Start by checking the installed WSL environments:

1
wsl --list --verbose

Example output:

1
2
3
4
  NAME                   STATE           VERSION
* Ubuntu-20.04           Running         2
  docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2

2. Stop the Distribution

Before compacting the disk, terminate the running distro:

1
wsl --terminate Ubuntu-20.04

3. Compact the Virtual Disk

Open diskpart, select the correct .vhdx, and compact it:

1
diskpart

Inside diskpart:

1
2
select vdisk file="C:\Users\<username>\AppData\Local\Packages\<distro>\LocalState\ext4.vhdx"
compact vdisk

Why This Is Worth Recording

This is the sort of systems task that is easy to half-remember. You know the disk exists somewhere, and you know there is a Windows tool involved, but the exact sequence tends to disappear from memory fast.

That is why I like turning short notes like this into posts. They are not dramatic, but they are useful.