Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To shrink a WSL2 disk, stop WSL and compact the correct ext4.vhdx file with Microsoft’s Optimize-VHD cmdlet or DiskPart’s compact vdisk command. Deleting Linux files alone usually does not reduce the VHDX file’s physical size in Windows: the virtual disk can retain its allocated blocks until they are compacted.

Why deleting WSL2 files may not free Windows storage

There are three different measurements to keep separate:

  • Linux filesystem use: space occupied by files inside the distribution.
  • Virtual capacity: the maximum size the guest filesystem can use.
  • VHDX physical allocation: the storage the disk file currently consumes on the Windows host.

A WSL2 distribution uses a dynamically expanding virtual disk. It grows as data is written, but removing files inside Linux does not necessarily return the corresponding blocks to Windows automatically. Compaction can reclaim eligible unused blocks and reduce the host-side file allocation; it does not, by itself, reduce the virtual disk’s maximum capacity.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not use the Linux-visible capacity as a measure of Windows storage used. Microsoft’s current WSL disk-space documentation describes a 1 TB default maximum allocation for recent WSL, while earlier releases may show 512 GB or 256 GB. These are version-dependent virtual-capacity defaults, not the physical size of your VHDX file. Microsoft: How to manage WSL disk space

#1 Best Overall
Sandisk 2TB Extreme Portable SSD, Up to 1050MB/s, USB-C, USB 3.2 Gen 2, IP65 Water and Dust Resistance, Updated Firmware, External Solid State Drive, SDSSDE61-2T00-G25
  • Get NVMe solid state performance with up to 1050MB/s read and 1000MB/s write speeds in a portable, high-capacity drive(1) (Based on internal testing; performance may be lower depending on host device & other factors. 1MB=1,000,000 bytes.)
  • Up to 3-meter drop protection and IP65 water and dust resistance mean this tough drive can take a beating(3) (Previously rated for 2-meter drop protection and IP55 rating. Now qualified for the higher, stated specs.)
  • Use the handy carabiner loop to secure it to your belt loop or backpack for extra peace of mind.
  • Help keep private content private with the included password protection featuring 256‐bit AES hardware encryption.(3)
  • Easily manage files and automatically free up space with the SanDisk Memory Zone app.(5). Non-Operating Temperature -20°C to 85°C

Check the distribution and identify its VHDX

First confirm which distribution is consuming space. If you have more than one, do not assume they share a disk or choose a VHDX based on a guessed path.

  1. Check the guest filesystem view. In PowerShell, Microsoft documents this command for checking WSL VHD filesystem capacity: wsl.exe --system -d <distribution-name> df -h /mnt/wslg/distro. Replace <distribution-name> with the intended distribution name. The result is the guest filesystem view; it is not the Windows file’s allocated size.
  2. Find the distribution’s base path. In PowerShell, query its registration entry under HKCU:SoftwareMicrosoftWindowsCurrentVersionLxss and use that distribution’s BasePath. The VHDX is typically at that base path with ext4.vhdx appended. Use Microsoft’s documented method to identify the intended distribution before operating on the file.
  3. Preserve important data. Make sure valuable distribution data is backed up before offline disk operations. Microsoft does not specify one universal backup procedure in its disk-space guidance.

Microsoft warns that modifying, moving, or accessing WSL files in AppData with ordinary Windows tools or editors can corrupt a distribution. Identify the VHDX through the distribution registration and perform the documented disk operation rather than opening or moving the file in Explorer or an editor. Microsoft: How to manage WSL disk space

Rank #2
Sandisk 1TB Portable SSD, Up to 800MB/s Read Speeds, Black (Old Model)
  • Solid state performance with up to 800MB/s read speeds in a portable drive. (Based on internal testing; performance may be lower depending on host device, interface, usage conditions and other factors. 1MB=1,000,000 bytes.)
  • Back up your content and memories on a storage solution that fits seamlessly into your mobile lifestyle.
  • Take it with you on your adventures—up to two-meter drop protection means this durable drive can take a beating. (Based on internal testing.)
  • Secure it to your belt loop or backpack for extra peace of mind thanks to the tough rubber hook.
  • From Sandisk, a brand professional photographers trust to take on assignments.

Compact the VHDX safely

Both documented compaction methods require WSL to be stopped before you work on its disk. Open PowerShell as appropriate for the selected method and run:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
wsl.exe --shutdown

This terminates all WSL instances. Substitute the actual path to the intended distribution’s ext4.vhdx wherever the examples below show <path-to-ext4.vhdx>.

Rank #3
SSK Portable SSD 500GB External Solid State Hard Drive USB C Up to 1050MB/s
  • Capacity Display Variance: 500GB external ssd often appears as around 465GB on Windows. MacOS can show full 500 GB capacity. This is binary calculation difference and doesn’t affect SSD hard drive actual physical storage
  • 1050 MB/s Speed: Instantly access to your files with blazing-fast 10Gbps external SSD read up to 1050MB/s and write up to 1000MB/s. LED Light indicates USB SSD instant activity
  • Data Security: Solid state drives S.M.A.R.T. health diagnostics​ and adaptive TRIM optimizing data block management ensures consistent write speeds and extends the longevity of the portable SSD
  • USB-C & USB-A Cable: Both cables featuring rapid USB 3.2 Gen2, this USB SSD effortlessly bridges devices, enabling seamless cross-platform file transfers and backup between computers, smartphones, tablets and iPhone
  • Always Fast: No slowdowns for large file transfers. With SLC caching (25% of current available capacity allocated as high-speed cache), this external SSD delivers steady 10Gbps for transfers within the cache capacity

Option 1: Optimize-VHD

Optimize-VHD is a Hyper-V PowerShell cmdlet. It requires the Hyper-V PowerShell module, and the target disk must be detached or read-only. Check Microsoft’s cmdlet documentation for the conditions and behavior of each mode before selecting one. Microsoft: Optimize-VHD

Optimize-VHD -Path "<path-to-ext4.vhdx>" -Mode Full

Full scans for zero blocks and reclaims unused blocks, but it is permitted only when the disk is read-only. Do not assume that a successful command will make the file smaller: Microsoft notes that optimization can complete without reducing the file size if no optimization is possible.

Rank #4
Sale
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400)
  • Easily store and access 2TB to content on the go with the Seagate Portable Drive, a USB external hard drive
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

Prezeroed is another mode for a detached disk when the cmdlet cannot query internal NTFS free-space information. It relies on blocks having been zeroed and is less effective. Follow the documented disk-state requirements for whichever mode you choose; do not treat the modes as interchangeable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Option 2: DiskPart compact vdisk

DiskPart’s compact vdisk reduces the physical size of a dynamically expanding VHD. The selected VHD must be detached or read-only. Run DiskPart from an elevated Windows terminal, then enter the commands with the actual VHDX path:

Best Value
Sale
Samsung T7 Portable SSD 1TB Titan Gray, USB 3.2 Gen 2, Up to 1,050MB/s
  • MADE FOR THE MAKERS: Create; Explore; Store; The T7 Portable SSD delivers fast speeds and durable features to back up any endeavor; Build your video editing empire, file your photographs or back up your blogs all in an instant
  • SHARE IDEAS IN A FLASH: Don’t waste a second waiting and spend more time doing; The T7 is embedded with PCIe NVMe technology that brings fast read and write speeds up to 1,050/1,000 MB/s¹, making it almost twice as fast as the T5
  • ALWAYS MAKE THE SAVE: Compact design with massive capacity; With capacities up to 4TB, save exactly what you need to your drive – from large working files to game data and everything in between
  • ADAPTS TO EVERY NEED: Whether using a PC or mobile phone, count on the T7 for extensive compatibility²; It’s a true team player when it comes to heavy-duty application usage or file-saving
  • HI RESOLUTION VIDEO RECORDING: Record Ultra High Resolution (4K 60fs) videos directly onto the T7 Portable SSD with your favorite camera or mobile devices; Supports iPhone 15 Pro Res 4K at 60fps video and more³
diskpart
select vdisk file="<path-to-ext4.vhdx>"
compact vdisk
exit

Confirm that the selected VHD is the intended distribution’s detached disk before compacting it. Microsoft documents this command for supported Windows versions, including Windows 10 and Windows 11. Microsoft: compact vdisk

Method Disk-state requirement What it changes Key distinction
Optimize-VHD Detached or read-only; Full specifically requires read-only. Requires the Hyper-V PowerShell module. Reclaims eligible unused blocks and can reduce physical allocation. Full scans zero blocks. Prezeroed is less effective and is for a detached disk when internal NTFS free-space information cannot be queried.
DiskPart compact vdisk Selected VHD must be detached or read-only. Reduces the physical size of a dynamically expanding VHD. It is a one-time compaction command, not a setting for ongoing automatic shrinking.
DiskPart shrink Acts on a selected supported volume; Microsoft documents NTFS or filesystem-free volumes among its constraints. Changes the size of a selected volume using free space at its end. It is not a command for compacting the Windows VHDX file. Microsoft: shrink
Resize-VHD See Microsoft’s cmdlet documentation for disk-state and resizing requirements. Changes virtual disk maximum capacity. It does not remove empty blocks; Microsoft directs users to Optimize-VHD for that purpose. Microsoft: Resize-VHD
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Verify the result on Windows

After compaction, check the VHDX’s physical size on the Windows host and compare it with its size before the operation. The Linux-visible filesystem capacity is not expected to change just because the file has been compacted. If the file did not shrink, that does not necessarily mean the command failed: there may have been no eligible blocks to reclaim, or the disk’s state and contents may not permit a reduction.

Can WSL shrink the disk automatically?

Microsoft’s WSL team described sparse VHD as an experimental setting in its September 18, 2023 update. The post said sparseVhd defaulted to false and, when enabled, applied automatically to newly created VHDs; it also documented wsl --manage <distro> --set-sparse <true/false> for existing distributions. Craig Loewen, a Microsoft Principal Product Manager, wrote that enabled sparse VHDs would “automatically shrink in size too!” Microsoft DevBlogs: Windows Subsystem for Linux September 2023 update

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

That 2023 description does not establish availability or behavior for every current Windows and WSL combination, including reclaim timing or how sparse disks interact with Hyper-V compaction. Check the current WSL release and configuration documentation on your machine before relying on automatic shrinking; no particular amount or schedule of reclaimed space is established here.

Quick Recap

Bestseller No. 2
Sandisk 1TB Portable SSD, Up to 800MB/s Read Speeds, Black (Old Model)
Sandisk 1TB Portable SSD, Up to 800MB/s Read Speeds, Black (Old Model)
From Sandisk, a brand professional photographers trust to take on assignments.
$188.90
SaleBestseller No. 4
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400)
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400)
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$129.99

Choose the right operation for the goal

  • Want to reclaim Windows storage after deleting Linux files? Stop WSL and compact the detached VHDX using one of the documented methods above.
  • Want to lower the virtual disk’s maximum capacity? That is a separate resize operation, not compaction. Microsoft’s current documentation says wsl --manage --resize requires WSL 2.5 or later; the cited instructions concern changing capacity, not reclaiming unused physical blocks.
  • Want to shrink a Linux volume? Volume shrinking is distinct from compacting the VHDX file. DiskPart’s shrink applies to a selected supported volume and is not a substitute for VHD compaction.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.