Removing bin and obj folder in .NET

less than 1 minute read

Building .NET applications through dotnet build produces the bin and obj folder. While running dotnet clean would only delete the files of bin folder only. In order to delete these folder when running dotnet clean, modify the project file and insert the code below

<Target Name="SpicNSpan" AfterTargets="Clean">
    <RemoveDir Directories="$(BaseOutputPath)" />
    <RemoveDir Directories="$(BaseIntermediateOutputPath)" /> 
</Target>

Once inserted, run dotnet clean these will automatically removed the bin and obj folder. Below shows illustrations before and after adding the configuration in the project file.

Before dotnet-clean-before Running dotnet clean doesn’t delete the bin and obj folders.

After dotnet-clean-after Running dotnet clean deletes the bin and obj folders.

Conclusion

In larger solutions that has multiple projects and some of the projects might reference to other projects, in some cases it needs to remove the obj folder in order to ensure that when dotnet build will capture all the code changes. However, most of the time MSBUILD take cares of it without requiring to delete these folders.