WSL ext4.vhdx 占用空间大

Posted by shensunbo on March 10, 2026

问题描述

WSL ext4.vhdx 占用空间很大,即使在虚拟机中删除了很多文件,但在windows侧ext4.vhdx并不会自动收缩,仍然占用大量磁盘空间。

解决方案

关闭虚拟机

1
2
3
4
5
6
PS C:\Users\***> wsl --shutdown
PS C:\Users\***> wsl --list --verbose
  NAME              STATE           VERSION
* Ubuntu-18.04      Stopped         2
  docker-desktop    Stopped         2
  Ubuntu-22.04      Stopped         2

使用diskpart工具压缩ext4.vhdx文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
PS C:\Users\vfvaf0> diskpart

Microsoft DiskPart version 10.0.19041.3636

Copyright (C) Microsoft Corporation.
On computer: DLFGVL3T3

DISKPART> select vdisk file="G:\vm\ubuntu22\ext4.vhdx"

DiskPart successfully selected the virtual disk file.

DISKPART> compact vdisk

  100 percent completed

DiskPart successfully compacted the virtual disk file.

DISKPART>

效果

  • 实际虚拟机大小:
    1
    2
    3
    
    PS C:\Users\vfvaf0> wsl -d Ubuntu-18.04  -- df -h /
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sdg       1007G   56G  901G   6% /
    
  • windows侧ext4.vhdx大小: wsl_before wsl_after

TODO wsl config 自动释放?maybe unsafe

  • .wslconfig
    1
    2
    
    [experimental]
    sparseVhd=true