October 22, 2018

Cleaning Visual Studio build output using the git clean command

Hey, before you start reading! I am in the market, looking for new freelance employment opportunities. If you need assistance on any of your ASP.NET Core projects, I am available for hire for freelance work.

Do you know the Clean Solution command in Visual Studio?

Clean Solution context menu

It turns out it is a pretty useless command.

I am not sure what it does exactly, but on my computer, it does not seem to ever actually delete any build artifacts. The problem is that from time-to-time things go a bit strange, and Visual Studio picks up some old artifacts - especially when working with Git and switching between a lot of branches.

In situations like these, you want to be able to clean all build artifacts to start fresh, and since the Clean Solution command is useless, a colleague of mine advised me to just use the git clean command.

Specifically, you can run it with the following parameters:

git clean -xdf

The x option deletes all untracked files, the d option deletes all untracked directories, and the f option will force the deletion of these files.

Be careful though, as this will nuke all files which are not tracked by Git, such as your user-specific settings files (*.suo, *.DotSettings.user, .vs folder etc.). If you’re using the old .csproj format that makes use of packages.config it will also delete your /packages directories meaning you will need to restore all NuGet packages as well.

For me, I don’t care too much about all these. By the time things get so bad that I feel I want to nuke all build output, I am more than happy to get rid of all those files as well to ensure that I make a fresh start.

Follow me on Twitter @jerriepelser for tweets about coding and building a Saas.