How to clean Visual Studio bin and obj folders

bin and obj folders visual studio
delete all bin
rm rf bin obj
delete output directory visual studio
clean solution visual studio
visual studio git clean
can i delete bin files
visual studio obj folder

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.

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.

Deleting All BIN & OBJ Folders in a Visual Studio Solution, Sometimes it turns out that we have to delete all bin and obj folders recursively in a Visual Studio solution. There's a built-in Visual Studio feature called Clean  Deleting All BIN & OBJ Folders in a Visual Studio Solution. Create an empty file and name it DeleteBinObjFolders.bat. Copy-paste code the below code into the DeleteBinObjFolders.bat. Move the DeleteBinObjFolders.bat file into the same folder with your solution (*.sln) file.

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)

Clean Solution, The bin and obj folders are usually safe to delete since they are automatically generated when the solution/project is being build by Visual Studio/  This one is plain and simple. Does nothing but delete these folders. You have the options to delete for the whole solution (bin,obj for every project, with or without the packages) or just for a project (bin and obj folders). Not another menu option hidden somewhere in the menu bar or one of its submenus.

For Visual Studio 2015 the MSBuild variables have changed a bit:

  <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="$(SolutionDir).vs" /> <!-- .vs -->
         <RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" /> <!-- obj -->
  </Target>

Notice that this snippet also wipes out the .vs folder from the root directory of your solution. You may want to comment out the associated line if you feel that removing the .vs folder is an overkill. I have it enabled because I noticed that in some third party projects it causes issues when files ala application.config exist inside the .vs folder.

Addendum:

If you are into optimizing the maintainability of your solutions you might want to take things one step further and place the above snippet into a separate file like so:

  <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
       <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="$(SolutionDir).vs" /> <!-- .vs -->
            <RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" /> <!-- obj -->
       </Target>
  </Project>

And then include this file at the very end of each and every one of your *.csproj files like so:

     [...]
     <Import Project="..\..\Tools\ExtraCleanup.targets"/>
  </Project>

This way you can enrich or fine-tune your extra-cleanup-logic centrally, in one place without going through the pains of manually editing each and every *.csproj file by hand every time you want to make an improvement.

Clean does not delete all files in bin and obj folders, VSmac 7.6 (build 2190). Doing a Clean, for the entire solution or the single project, does not delete all files in the obj and bin folders. The purpose of the Clean  In windows just open the explorer navigate to your SLN folder click into search field and type kind:=folder;obj--> for obj folders use CTRL+A and delete 'em - same for bin Done No need for any tool or extra software ;)

To delete bin and obj before build add to project file:

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

Here is article: How to remove bin and/or obj folder before the build or deploy

Easily Clean Bin/Obj Folders, It is time to clean up room on your Xamarin development machine by deleting those bin and obj folders with a simple script. It is time to clean up room on your Xamarin development machine by deleting those bin and obj folders with a simple script.

This site: https://sachabarbs.wordpress.com/2014/10/24/powershell-to-clean-visual-studio-binobj-folders/ uses powershell to remove any bin and obj folders from the current directory and sub directories. It should be possible to run it from the root of the drive Here are quotes from the article:

Here is Williams version

 gci -inc bin,obj -rec | rm -rec -force

In Williams own words

That wipes out all of the "bin" and "obj" directories in the current directory and every subdirectory. Super useful to run in your workspace directory to get to a "clean" state, especially when someone messes up and there’s something that a Clean or Rebuild inside the IDE doesn’t catch.

For those of you reading that may not know, PowerShell supports command aliases, here it is rewritten again not using the aliases

Get-ChildItem -inc bin,obj -rec | Remove-Item -rec -force

NOTE : You should have this stored in a PowerShell file and place that file at the root of your solution (where the .sln file resides), and then run it when you want a proper clean (not the micky mouse one that VisualStudio does, and reports success too).

Powershell To Clean Visual Studio Bin/Obj folders, read my last post, and alerted me to yet another useful PowerShell Command to clean all the BIN and OBJ folders of a Visual Studio solution. Home > Articles > How to remove bin and/or obj folder before the build or deploy How to remove bin and/or obj folder before the build or deploy In one of my previous articles, I explained How to: Turn on MVC compiled views or Turning on views compilation which can be very useful to detect potential errors in views code.

How to clean up BIN and OBJ folders in a Visual Studio , There may be better (and more elegant) ways of cleaning up the output (BIN) and intermediate (OBJ) folders generated by Visual Studio build  Adds command to clean bin and obj directories in solution. Writes messages to build output window. Also updates Visual Studio Status bar in progress

How to remove bin and/or obj folder before the build or deploy, However, once I've converted my projects to Visual Studio 2013 I star receiving error on build or deploy: It is an error to use a section registered  The 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.

A Visual Studio extension to really clean your projects, the "Clean solution" option of Visual Studio doesn't actually clean the solution. That is, it doesn't delete the bin and obj folders. And he was  Doing a Clean, for the entire solution or the single project, does not delete all files in the obj and bin folders. The purpose of the Clean operation is to clean everything up to start from a known state, so this does not work as intended.

Comments
  • Possible duplicate of I want to delete all bin and obj folders to force all projects to rebuild everything
  • This is the best solution, works, doesn't require special tools and makes sure every teammember uses the same "clean" method. Why MS Visual Studio doesn't clean properly is a complete mystery to me.
  • This is by far the easiest solution to implement and gets the job done without any external plugins
  • Not sure how to activate/call this target. Just choosing 'Clean' does not appear to do anything. So how do I use it?
  • sorry just now saw your comment. Specifying AfterTargets="Clean" will hook the target to the internal clean target for VisualStudio. However this simple target does not provide any feedbaco to the console so it will not do much other than wiping out your $(OUTDIR) from existence. You can add some error management and messages to it and get some better feedback.
  • I tried this in VS 2013 and it actually added folders to \bin & \obj. Caveat: my .csproj imports a few external .csproj files with build configurations - there are 26 builds, each of which goes in a subfolder under \obj / \bin (I can explain!).
  • This works beautifully. Very convenient when you want to do solution wide instead of just one Visual Studio project
  • @JoeHealy In stead of cleaning first and then zipping, it might be easier to use git-archive to zip directly from the repo.
  • Works like a charm! This is the best answer. Also if, let's say the project is not using source control, this answer still aplies: just add the project to source control (right click on solution name), then run the git command above. Btw, how do you use git commands in Windows? I had to use Bash on Ubuntu on Windows.
  • @brett Interesting ;) I typically use GitHub for Windows to pave (though I'm pretty sure VS installs a working rig that can be used from VS command prompt) - the settings lets you select a shell arrangement (from which I choose PoshGit). Using WSL is the most educational, so good call!
  • ok. now I see; the key is --dry-run, this will only tells you what's gonna be deleted. nice