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

bin and obj folders visual studio
delete all bin and obj folders powershell
command to delete all bin and obj folders
visual studio clean solution does not delete
how to clean project in visual studio code
visual studio exclude obj folder
delete bin and obj folders xamarin
command line delete all bin folders

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.

How to fully clean bin and obj folders within Visual Studio?, 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. Adds command to clean bin and obj directories in solution. Writes messages to build output window. Also updates Visual Studio Status bar in progress. Additionally you can clean directories from solution explorer. Right-click on project or solution: To add keyboard shortcut for the command specify shortcut keys for command CleanBinAndObj

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)

Deleting All BIN & OBJ Folders in a Visual Studio Solution, Closes all open documents; Collapses nodes in Solution Explorer; Activates Solution Explorer; Delete bin and obj folders; Super fast - you� 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 Solution, Extension for Visual Studio - A simple Visual Studio extension that adds an the bin and obj folders to both the context menus of solutions and C# If you do, you have probably already found some extensions here on the� 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.

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

Clean Bin, It is time to clean up room on your Xamarin development machine by deleting those bin Inside of each of my repo folders sits code and the bin/obj folders from each build, which can be very large as it contains the full excecutable. Forms Coffee Plugins NuGet Visual Studio C# XAML Android iOS Video� Note: Visual Studio may recreate bin and obj folders and subfolders after the operation. How to use. Build menu will have a sub-menu Brute Clean. This is enabled when the solution is open and not building. Clicking the menu will show you the solution folder that will be cleaned. After confirmation, it will clean the folder.

This site: https://sachabarbs.wordpress.com/2014/10/24/powershell-to-clean-visual-studio-binobj-folders/ uses William Kempf's powershell commands 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 is William's version

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

In William's 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).

Easily Clean Bin/Obj Folders, Here is Williams version In Williams own words That wipes out all of the “bin” and Powershell To Clean Visual Studio Bin/Obj folders you can use the following PowerShell commandlet to find the full commandlet name:� This can be gigs of data. Here is my GitHub folder: Inside of each of my repo folders sits code and the bin/obj folders from each build, which can be very large as it contains the full excecutable. What we can do is write a simple powershell script on Windows to recursively delete all of those nasty folders.

Powershell To Clean Visual Studio Bin/Obj folders, That is, it doesn't delete the bin and obj folders. I already have some extensions in the Visual Studio marketplace (and I You can check out the full source on Github — here's the gist of what I have learnt with this project:� When Visual Studio closes or the current solution is being manually closed, this extension will perform clean up. Closes all open documents. Collapses nodes in Solution Explorer. Activates Solution Explorer. Delete bin and obj folders. Super fast - you won't even notice it.

A Visual Studio extension to really clean your projects, Deleting All BIN & OBJ Folders in a Visual Studio Solution, Sometimes it turns out that we How to Empty Recycle Bin and Completely Delete Files/Folders, in� Clean does not delete all files in bin and obj folders visual studio for mac Marco Burato reported Aug 22, 2018 at 09:35 AM

How to clean Visual Studio bin and obj folders, Build clean should just remove the obj and bin directories completely. empty output directories have been left behind when cleaning a typical C# project. Notice in the script, that it only attempts to remove the bin output directory if a bin folder could not be cleared up at project clean. bin folder could be� The problem that occurs is the bin folder contains many third party dll's which are kept in the source before building the project. Those third party dll's are necessary to project. However after check-in to Azure DevOps the bin and obj are not present. This happens due to .gitignore and .gitattribute files. I have deleted both the files and

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.
  • @Rolf Visual Studio doesn't clean "properly" by default because MS has to support the use case where N different projects output to the same directory. If cleaning a project clobbered its entire output directory by default, it could clobber files entirely unrelated to that project. So instead the default behavior is to keep track of files that are actually output by the build process and delete only those files on clean. A common class of breakage at clean time is caused by creating or copying a file during the build without telling MSBuild to add it to the tracking list.
  • 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
  • Is there any nice way to delete all files in a folder except from one specific one?
  • @progLearner ofcourse there is stackoverflow.com/a/10019708/863651
  • Awesome! Thanks!