27

I got a VS project with a post-build event with a command-line command that copies a file (.dll) to the bin target dir (debug or release). When I do a "Clean" on the project every thing is cleaned, but this file remains. Is there a way to ad post-clean events so I can delete this file also?

Skagen
  • 273
  • 1
  • 3
  • 4
  • Possible duplicate of [How to create custom clean (post-clean) event in Visual Studio 2008?](https://stackoverflow.com/questions/631636/how-to-create-custom-clean-post-clean-event-in-visual-studio-2008) – StayOnTarget Nov 28 '18 at 18:51

8 Answers8

19

You can edit the project file directly and add the target to the end of the file. BeforeClean and AfterClean are targets as explained here:

https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-extend-the-visual-studio-build-process?view=vs-2019

You should be able to put a Delete task in the target.

EDIT Just tested this (right-click project -> unload -> right click -> edit) and the following target is what you need:

<Target Name="AfterClean">
    <Delete Files="$(TargetDir)\*.txt" />
</Target>

This works when you clean the project but not the solution - it works but not 100%.

Andy Braham
  • 9,594
  • 4
  • 48
  • 56
Leom Burke
  • 8,143
  • 1
  • 35
  • 30
16

Hat tip to @scrat789 regarding AfterTargets.

For VS 2017, v15.6.0 Preview 2.0, I ended up with the following:

  <Target Name="MyDistClean" AfterTargets="Clean">
    <Message Text="Deleting wwwroot\dist files" Importance="high" />
    <Delete Files="$(ProjectDir)\wwwroot\dist\*.*" ContinueOnError="true" />
  </Target>

A few things:

  • Notice I'm using a unique target name, MyDistClean. Specifying the name, AfterClean, did not work.
  • It wasn't necessary to reload the csproj after editing in order to run/test the Clean task.
  • Message importance is set high to avoid changing the MSBuild verbosity level mentioned here at SO: AfterClean Target Doesn't Get Invoked When I Clean The Project
  • MyDistClean task was processed at both the solution and project scope.

Here's the task improved to obliterate the webpack dist directory upon Clean:

  <Target Name="MyDistClean" AfterTargets="Clean">
    <ItemGroup>
      <DistDir Include="$(ProjectDir)wwwroot\dist" />
    </ItemGroup>
    <Message Text="Deleting @(DistDir)" Importance="high" />
    <RemoveDir Directories="@(DistDir)" />
  </Target>
bvj
  • 3,294
  • 31
  • 30
  • 2
    The name was my issue! AfterClean indeed does not work. Thx. – asdf Jun 19 '19 at 06:25
  • 1
    As of 2020/june and Microsoft Visual Studio 2019 Version 16.6.3 *this* approach is the one working. When you use any of the Target Name=Clean, AfterClean or BeforeClean it's **not** going to fly. You **must** use the AfterTargets=Clean attribute on a target you named with a name that's not in the reserved list shown at https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-targets?view=vs-2019 – Ger Hobbelt Jul 20 '20 at 17:33
11

I've found that Leom Burke's answer using "Delete Files" doesn't work with wildcards and doesn't tell you that anything went wrong. Here's what i've done instead -

<Target Name="BeforeClean">
        <Message Text="Cleaning other files..."/>
        <Exec Command="del $(ProjectDir)css\*.* /F /Q"/>
        <Exec Command="del $(ProjectDir)images\*.* /F /Q" />
        <Exec Command="del $(ProjectDir)js\*.* /F /Q" />
        <Exec Command="del $(ProjectDir)usercontrols\*.* /F /Q" />
        <Exec Command="del $(ProjectDir)MasterPages\*.* /F /Q" />
        <Exec Command="del $(ProjectDir)App_Data\TEMP\*.* /F /Q /S" />
        <Exec Command="del $(ProjectDir)App_Data\Logs\*.* /F /Q /S" />
    </Target>

I wrote the above to remove files in an umbraco solution so that when i do a diff with what's in source control, it doesn't confuse the snot out of me.

Frank Tzanabetis
  • 2,756
  • 2
  • 22
  • 22
10

You can do wildcards using built in Delete function.

<Target Name="AfterClean">
    <ItemGroup>
        <FilesToDelete Include="$(TargetDir)\*.txt"/>
    </ItemGroup>
    <Delete Files="@(FilesToDelete)" />
</Target>
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Davesmall28
  • 126
  • 1
  • 3
  • This is the most elegant solution. Watch what version of MSBuild you are on though. The older versions will process the ItemGroup when the document is loaded and race conditions can result in your files not being enumerated and deleted. – Christopher Painter Nov 19 '16 at 13:24
  • The reason MS make you do it this way is [explained here](https://blogs.msdn.microsoft.com/msbuild/2006/03/09/why-doesnt-delete-take-wildcards/). – David Rogers Mar 11 '19 at 20:27
6

I think something changes with Visual Studio 2017.

Works with tag : AfterTargets="Clean"

<Target Name="AfterClean" AfterTargets="Clean">
     <!-- Remove bin folder -->
     <RemoveDir Directories="$(TargetDir)" />
     <!-- Remove obj folder -->
     <RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" />  
</Target>
scrat789
  • 2,961
  • 3
  • 30
  • 26
1

You can update the csproj file to specify a new target. You will have to this in a text editor.

Take a look at the documentation here : How to: Extend the Visual Studio Build Process.

You will have especially to change the <Target name='AfterClean'>

Steve B
  • 36,818
  • 21
  • 101
  • 174
0

In continuation to other's tips - hope this will help also (took me some time to figure), can't bold in the code below, so notice the $(APPDATA) part..

...
    <Target Name="MyDistClean" AfterTargets="Clean">
        <ItemGroup>
            <DistDir Include="$(APPDATA)\MyApp\Configuration\" />
        </ItemGroup>
        <Message Text="Deleting @(DistDir)" Importance="high" />
        <RemoveDir Directories="@(DistDir)" />
    </Target>
</Project>
ShloEmi
  • 1,851
  • 2
  • 20
  • 25
0

Accepted answer did not work for me on Visual Studio 2022. Instead edit the project properties in Visual Studio:

  1. Right click on the project in VS
  2. Properties
  3. Configuration Properties, Advanced
  4. Extensions to delete on Clear: (example entry) $(SolutionDir)cmpdata\Output\$(Configuration)\$(Platform)\zdcom.dll;$(ExtensionsToDeleteOnClean)
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Farid Z
  • 960
  • 1
  • 9
  • 18