Fixing Normal Ubuntu Take 40GB Space Issue in WSL
If you're using the Windows Subsystem for Linux (WSL) and have noticed that your Ubuntu installation is taking up a lot of space, you're not alone. Many users have reported that their Ubuntu installation is taking up 40GB or more, even if they've deleted all their files and run the disk cleaning tool. In this article, we'll explain why this is happening and show you how to fix it.
Understanding the Issue
The issue arises because of the way WSL manages disk space. When you install Ubuntu on WSL, it creates a virtual disk that is 40GB in size by default. This virtual disk is stored in a file on your Windows filesystem, and it's not possible to resize it using standard tools. Even if you delete all your files and run the disk cleaning tool, the virtual disk will still be 40GB in size.
Checking Disk Usage
To check how much disk space your Ubuntu installation is using, open a command prompt and run the following command:
wsl --shutdown
This will shut down the WSL virtual machine. Then, open File Explorer and navigate to the following directory:
C:\Users\YourUsername\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs
Here, you'll see a file called ext4.vhdx. This is the virtual disk that Ubuntu is using. To check its size, right-click on the file and select Properties. You'll see something like this:
Fixing the Issue
To fix the issue, you need to create a new virtual disk with a smaller size. Here's how to do it:
- Open a command prompt as an administrator.
- Run the following command to create a new virtual disk:
wsl --shutdown
diskpart
create vdisk file=C:\Users\YourUsername\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows\_79rhkp1fndgsc\LocalState\rootfs
ew.vhdx maximum=20000 type=fixed
This will create a new virtual disk called new.vhdx with a size of 20GB. You can adjust the size to suit your needs.
- Run the following command to attach the new virtual disk:
select vdisk file=C:\Users\YourUsername\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows\_79rhkp1fndgsc\LocalState\rootfs
ew.vhdx
attach vdisk
This will attach the new virtual disk to WSL.
- Run the following command to copy the contents of the old virtual disk to the new one:
robocopy /mir C:\Users\YourUsername\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows\_79rhkp1fndgsc\LocalState\rootfs\ C:\Users\YourUsername\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows\_79rhkp1fndgsc\LocalState\rootfs-new
This will copy all the files from the old virtual disk to the new one.
- Run the following command to detach the old virtual disk:
detach vdisk
This will detach the old virtual disk from WSL.
- Run the following command to rename the new virtual disk:
rename vdisk file=C:\Users\YourUsername\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows\_79rhkp1fndgsc\LocalState\rootfs
ew.vhdx file=C:\Users\YourUsername\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows\_79rhkp1fndgsc\LocalState\rootfs\ext4.vhdx
This will rename the new virtual disk to ext4.vhdx, so that Ubuntu will use it.
Significance
By following the steps above, you can reduce the disk space used by your Ubuntu installation in WSL from 40GB to just a few GB. This can be useful if you're running low on disk space on your Windows system, or if you simply want to save some space.
References
-
Type: Online Resource
Title: How to resize a WSL 2 virtual disk
URL: https://docs.microsoft.com/en-us/windows/wsl/wsl2-disk-management#resize-a-wsl-2-virtual-disk
-
Type: Online Resource
Title: How to Reduce the Size of Your Ubuntu WSL 2 Virtual Machine
URL: https://www.how2shout.com/how-to/reduce-the-size-of-your-ubuntu-wsl-2-virtual-machine.html