How to reclaim disk space on Windows when using WSL
By adrienne
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: 1. Delete unnecessary files inside WSL. 2. 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:
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:
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:
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):
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:
This shuts down all running WSL distributions.
Step 6: Compact the virtual disk
Open Windows PowerShell as Administrator and run:
Then run the following commands, replacing the example path with the actual path to your virtual disk (that you got from Step 1):
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.vhdx` file 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:
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!