How to shrink WSL vhdx disk

The Windows utility diskpart can now be used to shrink Virtual Hard Disk (vhdx) files that is used to store Windows Subsystem for Linux (WSL).

I am putting the list of the instructions below.

First make sure all WSL instances are shut down by opening an administrator command window, and typing:

Verify everything is stopped by:

Then start diskpart:

and inside diskpart type:

For example:

it should respond by saying DiskPart successfully selected the virtual disk file.

Then to shrink

After this the vhdx file should shrink in usage. In my case it went from 40GB to 4GB. You can type exit to quit diskpart.

Want me to do this for you? Drop me a line: itgalaxyzzz {at} gmail [dot] com