The problem of WSL2 disk space not being released after deleting the container in Windows Docker
1. Causes and reasons
Many students found that the C drive was almost full after using the image for a period of time. They deleted the previously used images and containers and found that the size of the virtual disk in the WSL mount directory did not change, which was very strange. In fact, unlike WSL1, WSL2 is essentially a virtual machine , so Windows will automatically create a virtual disk file with the vhdx suffix as storage. The virtual disk file with the vhdx suffix is characterized by automatic expansion, but generally not automatic reduction . Once there are many files that “expand” it, it will not automatically “shrink” even if these files are deleted. Therefore, after deleting the files, we still need to manually compress them to free up disk space.
2. Solution
2.1 Find the virtual disk file to be compressed
If you did not change the location of the mounted disk, then its location is here C:\Users\{username}\AppData\Local\Docker\wsl\data\ext4.vhdx . Note the path as it will be needed later.