Deep clean your build

Monday, December 17, 2007 – 9:18 AM

I’ve never been completely happy with the way Visual Studio cleans solutions. If you take a look at how the Clean target is defined in Microsoft.Common.targets you’ll see its doing some clever stuff to look at what the last build produced and remove that. My experience is that over time and in the presence of custom build steps the bin and obj directories accumulate stale files.

In my book “Clean” should do just that, remove the build output folders and (obviously) their content.

How’s it done?

Define the following AfterClean target in a .targets file that gets imported after Microsoft.Common.targets in your project file and this definition of AfterClean will run after the default CoreClean and finish the job.

  <Target Name="AfterClean">
    <Message Text="Deep cleaning..." />
    <CreateItem Include="$(OutDir)*.*;$(IntermediateOutputPath)**\*.*">
        <Output TaskParameter="Include" ItemName="Clean_FilesToDelete"/>
    <Delete Files="@(Clean_FilesToDelete)" />
    <RemoveDir Directories="$(OutDir);$(OutDir)..\;$(IntermediateOutputPath);$(IntermediateOutputPath)..\" />

This not only removes all the files in bin and obj it also the folders themselves!

If you want to make this the default behavior machine wide the place this in $(MSBuildExtensionsPath)\v3.5\Custom.After.Microsoft.Common.targets. This is simpler than modifying your project file(s) but means that you cannot check these new targets in and deploy them as part of your source tree easily. Hence my approach in the previous post.

There’s a great article on MSDN about other things you can do to the build process How to: Extend the Visual Studio Build Process.

Technorati Tags: ,
  1. One Response to “Deep clean your build”

  2. Hi,
    Thank you for this, its been most helpful. Key thing is to have in Tools>Options the MSBuild verbosity to Normal to see the messages. That threw me until another google search helped out!

    By jradxl on Oct 9, 2010

Sorry, comments for this entry are closed at this time.