Bottom Line: You can safely shrink and compact ext4.vhdx to reclaim unused space. Do NOT delete the file directly unless you want to completely erase your entire Linux distribution (Ubuntu, Debian, Docker files).
What is the ext4.vhdx File?
Located at %LocalAppData%\Packages\CanonicalGroupLimited...\LocalState\ext4.vhdx (or under %LocalAppData%\Docker\wsl\data\ext4.vhdx), ext4.vhdx is the virtual hard disk file used by WSL2 (Windows Subsystem for Linux) and Docker Desktop.
The WSL2 Bloat Problem
WSL2 virtual disks dynamically expand as you install packages, download Docker images, or build projects in Linux. However, when you delete files inside Linux, WSL2 does not automatically shrink the .vhdx file on Windows. Over time, ext4.vhdx stays bloated at 30 GB, 60 GB, or 100 GB+, holding empty allocated space hostage.
Can You Delete ext4.vhdx?
- Deleting the file directly: ❌ Deletes your entire Linux system, all code, files, and Docker containers inside WSL2.
- Compacting / Shrinking the file: ✅ 100% safe. It compresses the file back down to match actual Linux storage usage without losing any data.
How to Safely Compact & Shrink ext4.vhdx
Step 1: Terminate WSL2
- Open PowerShell or Command Prompt.
- Run:
wsl --shutdown
Step 2: Compact the Virtual Disk via Diskpart
- Press
Win + Xand select Terminal (Admin) or PowerShell (Admin). - Launch
diskpart:diskpart - Select your
ext4.vhdxfile (replace with your exact path):select vdisk file="C:\Users\YOUR_USERNAME\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx" - Run the compact command:
compact vdisk - Once Diskpart reaches 100%, type
exit.
Alternative: Automated WSL Compact Command
In recent-items)-items)-items)-items)-items)-items)-items)-items)-items)-items)-items)-items)-items)-items)-items)-items) Windows 11 updates, Microsoft added a direct WSL compact command:
wsl --shutdown
wsl --manage Ubuntu --compact
What Happens After You Compact It?
- You instantly recover 10 GB to 80 GB+ of free disk space on Windows.
- All Linux files, packages, and Docker containers remain intact.
Related Guides
- Learn about deleting node_modules folders.
- How to clean up Docker data & overlays.
- Check if you can delete .git folders.
Discussion
Loading authentication...