Windows OS Hub
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux

 Windows OS Hub / Windows 11 / Shrink WSL2 and Docker Desktop VHDX Files in Windows

November 11, 2026

Shrink WSL2 and Docker Desktop VHDX Files in Windows

When Windows Subsystem for Linux (WSL2) is used extensively to run Linux distros or Docker Desktop containers on Windows, users may notice that the ext4.vhdx virtual disk files grow continuously. In some cases, this can consume all available free space on the host system drive.

By default, WSL2 uses VHDX virtual disk files, which expand dynamically when new data is written to them. However, these virtual disk files are not automatically compacted when data is deleted from them. In this guide, we’ll look at how to properly reduce the size of WSL virtual disk files, including Docker Desktop virtual disks, by shrinking them to optimize disk space usage.

Contents:
  • How to Compact a WSL VHDX Virtual Disk File in Windows
  • Reclaim Docker Desktop Disk Space in WSL2
  • Claiming WSL2 Disk Space with Sparse Mode

How to Compact a WSL VHDX Virtual Disk File in Windows

After unexpectedly running out of space on the Windows system drive, I ran the WinDirStat tool to analyze the storage usage and found a large ext4.vhdx virtual disk file in the user profile. The size of this WSL virtual machine VHDX file has increased to almost 55 GB (compared to an initial size of around 3 GB for a clean WSL2 Ubuntu image).

WSL2: ext4 vhdx eating my C drive

To check the amount of disk space being used by files inside the WSL image, run the following command in the Linux distro terminal:

df -h /

How much disk space is actually used in WSL2 distro

The size of the VHDX file increases dynamically as the guest operating system writes data to it. The guest OS in WSL sees a maximum disk size of 1 TB, regardless of the physical size of the hard drive on the host.

Let’s find out the exact location of the VHDX file for the specific WSL Linux distro. A Windows computer may have multiple WSL Linux images installed. List them:

wsl.exe --list --verbose

Copy the name of the Linux image (Ubuntu-24.04, for example), and extract information about its virtual disks from the system registry using PowerShell:

$wsl2distro='Ubuntu-24.04'
$VHDX=(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq $wsl2distro}).GetValue("BasePath") + "\ext4.vhdx"

In this example, the virtual disk file is stored in the user profile, which is the default WSL location: %USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu24.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx

Let’s use PowerShell to find out the actual size of a VHDX file on the system drive.

"{0:N2} GB" -f ((Get-Item $VHDX).Length / 1GB)

find wsl vhdx file size and location

As you can see, the VHDX virtual disk file is several dozen GB in size, even though only 3 GB of space is actually used within the Linux image. Clearly, the user has deleted files within the WSL distribution, but the storage space has not been reclaimed. Therefore, the VHDX disk needs to be compacted.

Before shrinking the VHDX file, it is advisable to find and remove any unnecessary files within WSL itself. The ncdu is a convenient tool for analyzing disk space usage within a Linux distro.

$ sudo apt-get install ncdu
$ sudo ncdu --exclude /mnt

ncdu - cleanup linux distro

Delete any unnecessary files, images, logs, and other junk data.

For example, I will also clean up obsolete package dependencies and cached installation files on Ubuntu/Debian:

$ sudo apt-get update && sudo apt-get autoremove -y && sudo apt-get clean

In order to compact the VHDX virtual disk, you first need to shut down the running WSL image:

wsl --shutdown

To compact the VHDX virtual disk, you first need to shut down the running WSL image:

Check that all WSL distributions are stopped:

wsl.exe --list -v

list wsl2

To compact the VHDX disk, you can use the built-in PowerShell Optimize-VHD cmdlet. For example:

Optimize-VHD -Path C:\Users\woshub\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu24.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx -Mode full

The Optimize-VHD cmdlet is only available in the Pro and Enterprise editions of Windows with the Hyper-V virtual machine management module installed (Hyper-V isn’t available on the Home edition of Windows. You can only enable it by using a trick.)

If this PowerShell cmdlet is missing, you can use the diskpart command to shrink a virtual VHDX disk.

diskpart

Specify the path to the VHDX disk:
select vdisk file="C:\Users\woshub\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu24.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx"

Mount the VHDX file in read-only mode:

attach vdisk readonly

Shrink the file

compact vdisk

Unmount the VHDX file:

detach vdisk
exit

diskpart compact WSL vhdx file

Verify that, after compacting the VHDX file, its actual size on the physical disk has been significantly reduced and is now close to the amount of data actually stored in WSL

If necessary, you can move any installed WSL distro image file from the system volume to another drive.

Reclaim Docker Desktop Disk Space in WSL2

If you have Docker Desktop for Windows installed on a computer, it also uses WSL2 to run containers and stores its data in a VHDX virtual hard disk. This VHDX file grows continuously when images, containers, and volumes are added. However, it does not shrink the virtual disk automatically when this data is removed.

To display the amount of disk space actually being used by Docker Desktop:

docker system df

The free space inside the Docker Desktop VHDX file can also be reclaimed using the Optimize-VHD cmdlet or the diskpart tool. However, first remove any unused Docker images and containers to clean up the disk from inside the WSL guest:

docker system prune -a

By default, Docker’s WSL data is stored in the file %LOCALAPPDATA%\Docker\wsl\disk\docker_data.vhdx .

Close Docker Desktop and shut down WSL:

wsl --shutdown

You can then compact the docker_data.vhdx file as described above. For example:

Optimize-VHD -Path "$env:LOCALAPPDATA\Docker\wsl\disk\docker_data.vhdx" -Mode Full

Claiming WSL2 Disk Space with Sparse Mode

WSL2 offers an experimental feature that can automatically return unused disk space within a VHDX file to the host system. A sparse VHDX feature leverages NTFS and Hyper-V virtualization features to enable TRIM/UNMAP operations against VHDX files (there is an increased risk of file fragmentation when using this feature on HDDs). Essentially, the sparse mode can automatically compact the WSL2 virtual VHDX disk on the fly.

Sparse mode is still an experimental feature and, in some scenarios, may cause corruption of the file system on both the NTFS host side and the guest ext4 file system. Therefore, it is not suitable for use in production environments.

You can enable sparse mode to automatically reduce the VHDX size for a specific WSL2 distro using the command:

wsl --manage Ubuntu-24.04 --set-sparse true

A warning will be displayed when you run the command:

Sparse VHD support is currently disabled due to potential data corruption.
To force a distribution to use a sparse VHD, please run:
wsl.exe --manage <DistributionName> --set-sparse true --allow-unsafe
Error code: Wsl/Service/E_INVALIDARG

Sparse VHD support is currently disabled due to potential data corruption. To force a distribution to use a sparse VHD, please run: --allow-unsafe

To ignore the warning and force-enable experimental sparse mode, run:

wsl --manage Ubuntu-24.04 --set-sparse true --allow-unsafe

You can also enable sparse mode by editing the configuration file (%UserProfile%\.wslconfig).

[experimental]
sparseVhd=true

Once this option is enabled, the VHDX file will be automatically shrunk after data within it is deleted, without the need to stop the WSL virtual machine.

Before enabling sparse mode, optimize the VHDX file size using diskpart or Optimize-VHD. After sparse mode is enabled, you will no longer be able to compact the file using these commands.

0 comment
0
Facebook Twitter Google + Pinterest
Questions and AnswersWindows 11
previous post
How to Enable Password Sign-In Instead of PIN in Windows 11

Related Reading

How to Move (Migrate) Windows Shares to a...

February 26, 2026

Security Warnings When Opening RDP Files in Windows...

April 20, 2026

Monitor Windows Log Files in Real Time with...

March 26, 2026

Remove the Max Path Length Limit (260-Characters) on...

November 19, 2025

Automate Software and Settings Deployment with WinGet Configure...

November 6, 2026

CrowdSec on Windows: From Installation to Threat Blocking

July 6, 2026

We Can’t Sign In to Your Account: Fix...

October 29, 2026

Updating UEFI Secure Boot Certificates on Windows Devices...

April 27, 2026

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • We Can’t Sign In to Your Account: Fix Temporary User Profile in Windows

    October 20, 2026
  • Backup/Sync to Cloud Storage with Rclone on Windows

    October 9, 2026
  • Fix Missing “Yes” Button in User Account Control Prompt on Windows

    October 7, 2026
  • Block Specific Keyboard Shortcuts with Keyboard Filter on Windows

    October 3, 2026
  • How to Set Task Priority in Windows Task Scheduler

    September 22, 2026
  • AutoStart Program at RDP Logon in Windows RDS

    September 8, 2026
  • How to Convert Windows Machine into a Proxmox VM

    August 30, 2026
  • Enable or Disable Fast User Switching in Windows 11

    August 18, 2026
  • How to Hide Wi-Fi Network in Windows with WLAN Filters (Blacklist and Whitelist)

    August 14, 2026
  • Invalid Signature Detected: Check Secure Boot Policy [Fix]

    August 5, 2026

Follow us

  • Facebook
  • Twitter
  • Youtube
  • Telegram
Popular Posts
  • Converting Windows 10 to Enterprise LTSC Without Losing Data
  • How to Remove ‘Some Settings are Managed by Your Organization’ on Windows 11 or 10
  • Remove the Max Path Length Limit (260-Characters) on Windows
  • Installing Windows without USB/DVD or Other External Drives
  • Configure Autostart and Boot Order for VMs on Proxmox
  • Uninstalling Windows Updates via CMD/PowerShell
  • Fix: Your PIN Isn’t Available When Signing in Windows 11
Footer Logo

@2014 - 2026 - Windows OS Hub. All about operating systems for sysadmins


Back To Top