TFS build - post build cleanup - recursively delete wildcard files using MSBuild.proj

I have seen many requests over the years asking for help in doing post build cleanup for TFS Build.

There are many approaches for achieving this. Each has it’s own pros and cons. I would lean towards simplicity of approach and maintenance.

Assuming that you have to delete all dll’s that start with test. Modify your TFSBuild.proj file and add the following lines at the very end (just before closing </Project>):

 <Target Name="AfterDropBuild">
    <ItemGroup>
        <FilesToDelete 
            Include="$(DropLocation)\$(BuildNumber)\**\test*.dll" />
    </ItemGroup>
    <Delete 
        Files="@(FilesToDelete)" 
        ContinueOnError="true" 
        TreatErrorsAsWarnings="true"/>
</Target>

Notes:

  1. If you would like to recursively find these files, make sure to use the ** convention in the Include attribute.
  2. If you want any specific build variable (e.g. $(BuildNumber) in my example), make sure to do the file collection inside the Target.