Reclaiming disk space for the dotnet developer

Posted by ryansouthgate on 26 Jul 2023

This is a short and sweet post, reminding myself (and you too) of a few ways to reclaim Drive space on your development machine. As a dotnet developer, we have to install a few tools and SDKs. Packages we use (through NuGet or Packet) are downloaded/cached locally and don’t get cleaned up. Below are a few steps which help reclaim some of that space. Most of these are windows-centric, however some can also be used on Linux machines too.

Remove downloaded NuGet packages

This will empty your local NuGet cache. You don’t need local copies of old dotnet packages from years ago, so run this command and save some space

dotnet nuget locals all --clear

This command cleaned 14.2GB for me

Uninstall/delete SDKs/Programs/Files you no longer use

I’m not going to treat you like an idiot here, you know how to uninstall programs and delete files you’re not using. This is just a reminder to go through what you have installed, and remove all that junk.

I’ve been an avid user of WinDirStat for years. It’s always worked great! It scans your drive and shows you large files and where they are living. However, more recently I’ve started using WizTree. WizTree is quick! WinDirStat takes about 20 mins on my 500GB NVMe Drive, whereas WizTree takes less than 30 seconds, WinDirStat hasn’t been updated in a long time, and while it still works, it’s definitely aging and has fallen behind a bit.

This kind of tool has helped me free up tons of space. Recently, I had deleted a VM (Windows 11), however the .vhdx (Virtual Hard Disk) file hadn’t been deleted along with the VM profile. So a 41GB file was just hiding on my C:\ drive, taking up space. A regular run of WizTree showed a huge “rectangle” of space, which was the .vhdx file, knowing that I no longer wanted that VM, I just deleted the file and reclaimed that space. You can see what this looked like in WizTree below

The WizTree interface showing a large .vhdx file

WSL2 vhds grow large

I have 2 WSL2 Linux distributions on my Windows 10 laptop; Ubuntu and Debian. I use these primarily for writing/running scripts. Sometimes I use these along with ffmpeg to change the formats of video files I own.

For different distributions, the paths where the .vhdx files will be, will likely be different. For me, the easiest way to find these growing files is to use WizTree (above). Once the files are located, open a PowerShell window in the parent directory of each file, and run the following commands… replacing “ext4.vhdx” with your vhdx file

If you have Docker running, then it’s highly recommended to shut it down before running these commands. There’s an issue on GitHub where people have reported it corrupting their Docker installs and taking their containers with it.

wsl.exe --shutdown
cd C:\Users\ryan\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_\LocalState\
optimize-vhd -Path .\ext4.vhdx -Mode full

Old Docker images and containers

I use Docker a lot when developing. I prefer building/deploying my applications using Docker for reproducible and repeatable builds/deployments. All base images that are used in construction of your containers are cached locally, and when you update an image, it also gets cached too, and takes valuable drive space.

The below command removes all unused containers, networks, images. There is also an option to remove volumes, however, my volumes never get too large, and I’d prefer to keep these and their data. Whereas images can be downloaded, and containers created again, easily.

docker system prune -a

Old Faithful: Windows Disk Cleanup

This is a classic. Windows Disk Cleanup should be run regularly and simply cleans junk files Windows stores on your machine, whilst downloading Windows Updates, installing programs etc

Run the command below. The flag “sagerun” enumerates all drives and lets Windows automatically clear out old Windows Update files and junk.

cleanmgr /sagerun

I hope this has freed up some drive space for you. As always, I hope this helps and thanks for reading



comments powered by Disqus