Article
How to reclaim disk space on Windows when using WSL
If you've been vibe coding on a Windows PC using WSL (Windows Subsystem for Linux), you may be surprised by how much disk space your development environment takes up.
I recently got a low disk space warning in Windows and took the usual steps: deleted temporary files, emptied recycle bin, moved large files to cloud storage, deleted unused programs, etc. But those steps only freed up a few gigabytes of space.
What I hadn't realized was how large my development environment had gotten because it doesn't show up as a category inside of Windows storage settings.
I recently went through an exercise to shrink my WSL development environment which freed up over 30 GB of diskspace on my Windows PC and wanted to share a simple guide, if you'd like to do the same thing.
Why this happens
WSL stores your Linux filesystem inside a virtual hard disk file, typically called ext4.vhdx.
As your dev environment grows, this file grows. When you clone repos, install dependencies, build applications and generate caches, you're using diskspace that is reflected in this file. When you look at your Windows storage settings to see what is taking up disk space, you will not see the impact of your WSL dev environment because it's not located with your documents or photos, so you may not realize how much space it's actually eating up:
Even after you delete files inside your development environment (WSL), the virtual disk won't shrink automatically so it doesn't impact the actual space available on your Windows PC.
Reclaiming disk space is therefore a two-part process:
- Delete unnecessary files inside WSL.
- Compact the virtual disk to return the unused space to Windows.
Step 1: Locate your virtual disk to get a baseline
Open Windows PowerShell and run:
Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss |
Get-ItemProperty |
Select-Object DistributionName, BasePath
This command lists your installed Linux distributions (such as Ubuntu) and their storage locations on your Windows PC.
Look for the ext4.vhdx file and note its full path (you'll need it in step 6 below). Navigate to its directory and check the current size of this file before proceeding so you can compare it afterward.
You may be surprised at how large it is!
Step 2: Find out what's taking up space
Open your WSL terminal and run:
du -h --max-depth=1 ~ | sort -hr
This shows the largest folders in your Linux home directory, including hidden directories.
Step 3: Clean up unnecessary files
This next step is where the real action happens, and I recommend asking your AI to help rather than trying to figure out what to delete on your own.
You can tackle this across your entire WSL home directory (which may contain all your repos, shared caches and development tools), or you can work through your repos one by one.
Here's a simple prompt you can try:
I'm trying to reclaim disk space in my WSL environment. Analyze my directory structure and identify large files, folders, dependencies, build artifacts and caches that I can safely remove. Show me how much space each is consuming, how much space I could potentially recover, and whether there are any consequences to deleting it.
Do not delete source code, uncommitted changes, local databases, environment files, credentials, configuration files or anything that cannot be easily regenerated. Flag anything you're unsure about and ask for my approval before deleting anything.
Common culprits your AI may identify include:
- node_modules folders in inactive repos
- .next, dist and other generated build directories
- npm, pip and other package manager caches
- Old Docker images and containers
- Old logs and temporary files
- Repos you no longer need
Many of these files will be regenerated or downloaded again the next time you work on a project, but for projects you are not working on frequently, there's no reason to let them continue to eat up space.
Review your AI's recommendations, ask questions if you're unsure about anything, and approve the cleanup as you go.
Step 4: Mark the freed space as unused
From your WSL terminal, run (in any directory is fine):
sudo fstrim -av
This helps the virtual disk identify storage blocks that are no longer needed.
Step 5: Shut down WSL
Save your work and close VS Code, Docker Desktop and any other applications using WSL.
In Windows PowerShell, run:
wsl --shutdown
This shuts down all running WSL distributions.
Step 6: Compact the virtual disk
Open Windows PowerShell as Administrator and run:
diskpart
Then run the following commands, replacing the example path with **the actual path to your virtual disk **(that you got from Step 1):
select vdisk file="C:\your\actual\path\ext4.vhdx"
compact vdisk
exit
You can run each line one at a time, or all at once. Depending on the size of your virtual disk, the compact step may take several minutes.
You can then restart WSL normally.
**Important: **Never delete the
ext4.vhdxfile itself. It contains your entire Linux filesystem.
Step 7: Check your results
Compare the new size of your ext4.vhdx file to what it was previously.
Go to Windows Settings → System → Storage to see how much disk space you've reclaimed.
I was able to reduce mine from 65 GB to 35 GB.
Or just ask your AI
If you already work with an AI, it might be even easier to just use this prompt and let your AI guide you through the entire process:
I'm developing on Windows using WSL 2, and I suspect my WSL virtual disk is consuming excessive disk space.
Walk me through the process of reclaiming disk space, one step at a time:
1. Identify how much Windows disk space my WSL installation is consuming.
2. Analyze which files, dependencies and build caches inside WSL are taking up the most space.
3. Recommend what I can safely delete without losing source code, configuration, credentials or important data.
4. Help me compact the WSL virtual disk so the freed space is returned to Windows.
5. Verify how much space I reclaimed.
Give me the exact commands to run, tell me whether to run them in WSL or Windows PowerShell, and wait for my output before proceeding to the next step. Do not delete anything without my explicit approval.
Everyone's development environment is a little different, so having your AI guide you through the process based on your specific setup may be the easiest approach.
At one point while I was doing this activity, my AI suggested I run a command to avoid having to do manual clean up again. It suggested I run wsl --manage <distro> --set-sparse true to have the disk reclaim space and shrink automatically in the future. However, this feature (sparse VHD) is still classified as experimental and people have reported issues with it, so I'm planning to stick to the manual cleanup for now.
Happy vibe coding and enjoy your reclaimed disk space!