Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fully clean bin and obj folders within Visual Studio?

If you right click on a folder, you will see a "Clean" menu item. I assumed this would clean (remove) the obj and bin directory. However, as far as I can see, it does nothing. Is there another way? (please don't tell me to go to Windows Explorer or the cmd.exe) I'd like to remove the obj and bin folder so that I can easily zip the whole thing.

like image 269
tom7 Avatar asked Jul 06 '09 18:07

tom7


People also ask

Can I delete bin and obj folders Visual Studio?

Delete bin and obj foldersThe bin and obj folders are usually safe to delete since they are automatically generated when the solution/project is being build by Visual Studio/MSBuild. This feature is off by default, but can easily be enabled in the settings.

What are the bin and obj folders?

In summary, in “obj” folder, we have compiled files for each source code file and in “bin” folder, we have a single unit which links all individually compiled code files. Below is a 10 minute YouTube video which demonstrates how both these folders look like and how incremental compilation happens.

Where is the bin folder Visual Studio?

You can open the output bin directory on any project node in Visual Studio by simply right-clicking the project node and selected the "Open bin Folder (Explorer)" menu option.


2 Answers

As others have responded already Clean will remove all artifacts that are generated by the build. But it will leave behind everything else.

If you have some customizations in your MSBuild project this could spell trouble and leave behind stuff you would think it should have deleted.

You can circumvent this problem with a simple change to your .*proj by adding this somewhere near the end :

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

Which will remove everything in your bin folder of the current platform/configuration.

------ Edit Slight evolution based on Shaman's answer below (share the votes and give him some too)

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

---- Edit again with parts from xDisruptor but I removed the .vs deletion as this would be better served in a .gitignore (or equivalent)

Updated for VS 2015.

<Target Name="SpicNSpan" AfterTargets="Clean"> <!-- common vars https://msdn.microsoft.com/en-us/library/c02as0cs.aspx?f=255&MSPPError=-2147217396 -->      <RemoveDir Directories="$(TargetDir)" /> <!-- bin -->      <RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" /> <!-- obj --> </Target> 

He also provides a good suggestion on making the task easier to deploy and maintain if you have multiple projects to push this into.

If you vote this answer be sure to vote them both as well.

like image 156
Newtopian Avatar answered Oct 24 '22 09:10

Newtopian


If you are using git and have a correct .gitignore in your project, you can

git clean -xdf --dry-run 

to remove absolutely every file on the .gitignore list, i.e. it will clean obj, and bin folders (the x triggers this behavior)


Note: The parameter --dry-run will only simulate the operation ("Would remove ...") and show you what git would delete. Try it with dry-run, then remove the parameter and it will really delete the files+folders.

Optionally, after that clean command, you can use dotnet restore mySolution.sln to get all the NUGET packages restored. And if you have a developer console open anyway,
you can quickly run msbuild -m mySolution.sln afterwards (without having Visual Studio open) to see if it was successful.

like image 23
Ruben Bartelink Avatar answered Oct 24 '22 09:10

Ruben Bartelink