Safe to Delete

WSL2 Virtual Hard Disk (ext4.vhdx)

Quick Answer: ext4.vhdx is the virtual disk for Windows Subsystem for Linux (WSL2). Shrinking or compacting it is safe and recovers tens of gigabytes of unassigned space.

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

  1. Open PowerShell or Command Prompt.
  2. Run:
    wsl --shutdown

Step 2: Compact the Virtual Disk via Diskpart

  1. Press Win + X and select Terminal (Admin) or PowerShell (Admin).
  2. Launch diskpart:
    diskpart
  3. Select your ext4.vhdx file (replace with your exact path):
    select vdisk file="C:\Users\YOUR_USERNAME\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx"
  4. Run the compact command:
    compact vdisk
  5. 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.

Discussion

Loading authentication...

Related in developer

The AI Engineer's Model Disk Cleanup Guide: Hugging Face, Ollama & PyTorch

The ultimate 1,200-word field guide for AI developers and local LLM hobbyists to reclaim 200GB+ from Ollama, Hugging Face, PyTorch, and vLLM model weights.

Safe to Delete

Android Studio System Index Cache (AndroidStudio Cache)

Android Studio caches Gradle project indexes, VFS caches, and compiler artifacts. Invalidate and clear is safe.

Safe to Delete

Ansible Automation Temp & Fact Cache (~/.ansible/tmp)

Ansible stores temporary playbook execution modules and target host fact JSON caches in ~/.ansible/tmp. Clearing Ansible tmp is safe.

Safe to Delete
Back to all files