'Custom .csproj target to delete output directories not run every time

I'm working on a custom ASP.NET Core project. I added the following snippet to my client's .csproj file to delete the previous build's artifacts when building:

<Target Name="RemoveFiles" BeforeTargets="BeforeBuild" Condition="'$(Configuration)' == 'Release'">
    <Message Text="Cleaning bin and obj directories..." Importance="high" />
    <RemoveDir Directories="$(ProjectDir)bin;$(ProjectDir)obj" />
    <Message Text="Restoring NuGet packages..." Importance="high" />
    <Exec Command="dotnet restore" />
</Target>

Unfortunately, the snippet doesn't appear to be run every time I publish but only intermittently. Does anyone have any idea why this might be the case?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source