Docs: dotnet clean command won't delete bin and obj folder that produced by dotnet publish

Created on 11 May 2019  Â·  15Comments  Â·  Source: dotnet/docs

I tried to publish my console program to another folder for output, but i found there are still bin and obj exists in the source folder. When i run "dotnet clean", it won't delete these two folders in my src folder.
thanks


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - .NET Core Guide Technology - CLI P1 doc-bug dotnet-cltech dotnet-corprod

Most helpful comment

@1jeffchristensen Hello.

However, I have projects that also use project references and when I do dotnet clean everything is deleted from the build bin/obj folder. It's completely empty for me, because I don't do anything extra like copy more files into the output folder (outside of a normal reference file). Are you copying files to these folders? the dotnet clean command parses the previous build output for the project and cleans out everything known about the project. For example, if you have file1.txt referenced in your project as an asset to copy to the output folder, it will be deleted from the output folder with dotnet clean. However, if you just manually copy that file into the folder, dotnet clean isn't going to touch it, and that is a good thing.

If you need to go further and delete the entire bin/obj folders, I suggest you just make a small script and run that instead of dotnet clean. Or add a new task to your project that runs after the dotnet clean command that deletes those folders for you. For example:

```xml



All 15 comments

@livarcocc @KathleenDollard according to this comment, it seems that dotnet clean only cleans build content and not publish content. Is that behavior by design? Do we intend to change this in future releases?

That is by design for now. We currently do not have plans to change this behavior.

cc @peterhuene in case he has something else to add.

@quantrpeter As this is by design, I suggest you file an issue with https://github.com/dotnet/cli/issue and request that clean behaves this way (or some option to toggle it)

I'm going to close this as the bug isn't related to a problem with the documentation. Thanks!

Reopening since the docs are saying the folders are cleaned. We should describe the correct behavior.

/cc @cartermp @terrajobst

Reading it again I think the description is correct:

Only the outputs created during the build are cleaned. Both intermediate (obj) and final output (bin) folders are cleaned.

This is true and is what indeed happens. Only the files generated for a build are deleted. It doesn't state that the bin folder is deleted. Per this description, publishing (being a whole other process that puts the published files into a folder under bin) wouldn't be affected.

Yep, but we need to make that more clear (meaning I just want to add a sentence talking about the publish folders not being cleaned explicitily).

Setup:
In folder D:\dev\myapp\ I have my sln file and no bin folder and no obj folder.
I run dotnet --version which outputs SDK 3.1.100

Step 1:
Then I run dotnet build from within D:\dev\myapp
Now I have a bin folder and a obj folder.

Step 2:
Now I run dotnet clean from within D:\dev\myapp

Issue:
I still have a bin and obj folder. (And I have a built dll for a referenced project that will never get updated unless I go manually the bin folder from my file system.)

This is so problematic! A dll built in bin\Debug\netcoreapp will now exist for a referenced project, but if I go make a change in the referenced project, the cli will NOT produce a new dll!

Even though the GitHub issue 12304 is about dotnet clean, I have identified several issues plaguing my team.

  1. I believe that dotnet clean should clean what was just built using dotnet build seconds earlier.
  2. dotnet build will no longer use the referenced project as source since there is already a built dll for the project, even though that dll is out of date.
  3. The documentation for dotnet clean does not match the stated behavior when there are referenced projects in the csproj.

@1jeffchristensen Hello.

However, I have projects that also use project references and when I do dotnet clean everything is deleted from the build bin/obj folder. It's completely empty for me, because I don't do anything extra like copy more files into the output folder (outside of a normal reference file). Are you copying files to these folders? the dotnet clean command parses the previous build output for the project and cleans out everything known about the project. For example, if you have file1.txt referenced in your project as an asset to copy to the output folder, it will be deleted from the output folder with dotnet clean. However, if you just manually copy that file into the folder, dotnet clean isn't going to touch it, and that is a good thing.

If you need to go further and delete the entire bin/obj folders, I suggest you just make a small script and run that instead of dotnet clean. Or add a new task to your project that runs after the dotnet clean command that deletes those folders for you. For example:

```xml



Could we add a new command called dotnet purge that deletes these folders?

@lukeschlather You would want to file a request on https://github.com/dotnet/sdk/ as this github repo is just for documentation related to how the CLI works. New features and/or bugs should be filed at the repo I just linked to. Cheers!

thank you all gentlement, thanks a lot

You actually wanna file the issues at https://github.com/dotnet/sdk, since the CLI repo has recently moved

Thanks @mairaw .. I keep doing that. I updated my comment.

@Thraka's suggestion was very helpful but needed some tweaking to get it to run with latest dotnet core with a standard dotnet clean command. To save anyone else the trouble, you can drop this element right into your csproj file and it should work.


<Target Name="PostClean" AfterTargets="Clean">
    <Exec Command="rm .\bin -r" />
    <Exec Command="rm .\obj -r" />
    <Exec Command="echo Hit it with hammer clean complete" />
</Target>

The rm command is not cross-platform (err, not cross-shell). I propose this more portable solution.

<Target Name="PostClean" AfterTargets="Clean">
    <RemoveDir Directories="$(BaseIntermediateOutputPath)" /><!-- obj -->
    <RemoveDir Directories="$(BaseOutputPath)" /><!-- bin -->
</Target>

The build properties are (partially?) enumerated here:
https://docs.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties?view=vs-2019

Was this page helpful?
0 / 5 - 0 ratings

Related issues

svick picture svick  Â·  3Comments

tswett picture tswett  Â·  3Comments

FrancescoBonizzi picture FrancescoBonizzi  Â·  3Comments

mekomlusa picture mekomlusa  Â·  3Comments

skylerberg picture skylerberg  Â·  3Comments