Kash Farooq's software development blog

.NET Developer

Cleaning all bin and obj files with MSBuild

Posted by Kash Farooq on February 26, 2012

I thought I’d blog this as it took me a frustratingly long time to work out how to do it (I’m new to MSBuild scripts – I usually do my builds with nant).

I wanted to simply clean my solution before the build. The solutions I found on Stack Overflow were unsatisfactory. For example, I didn’t want to have to name the folders that I wanted to delete files from.

So, here’s what I did; I simply called the “Clean” MSBuild target for all csproj files found below the directory the build was initiated from:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build-and-test">

  <Target Name="Build-and-test">
    <CallTarget Targets="clean-all-folders"/>
    <CallTarget Targets="build"/>
    <CallTarget Targets="unit-tests"/>

    <ProjectFiles Include="**\*.csproj" />

  <Target Name="clean-all-folders">
    <MSBuild Projects="@(ProjectFiles)" targets="Clean" />


This doesn’t delete the bin, debug and obj folders – but I don’t believe that is a problem.

The above script is generic – it doesn’t care what the project files or solution are called.

You can eliminate the ItemGroup if you are willing to hard code your solution file:

<Target Name="clean-all-folders">
   <MSBuild Projects="MySolition.sln" targets="Clean" />

Sorry, the comment form is closed at this time.

%d bloggers like this: