[Crosspost from my MSDN blog]
When cleaning up drive space, the first thing I do is remove the ‘obj’, ‘bin’, and ‘packages’ directories from my development directories. They are temporary and will be rebuilt the next time I build the related project. Because I end up with a lot of little test & sample projects that I don’t refer to often, their binaries and nuget directories are just taking up space.
The reason this is better than doing a Clean Solution is that Clean Solution only removes the outputs of the build. It doesn’t remove the nuget packages which in my case were a significant percentage of my overall dev directory space.
The old way
I used to do this with a little Windows Explorer trick – search filters. It looks like this
“type:folder name:obj” tells explorer to find all the items that have “obj” in the name and are folders. Then I can easily “Select All” and delete. Then repeat for Bin and for Packages. (There is one caveat here that the name search is a substring search, so it will also return directories named “object” too.)
The PowerShell way
But today I got to thinking. I wanted to do that in one step. So here is a PowerShell command that will iterate from the current directory and find all child directories named ‘obj’, ‘bin’, or ‘packages’ and prompt me to delete them.
get-childitem -Directory -Recurse | where-object {$_.Name -eq ‘bin’ -or $_.Name -eq ‘obj’ -or $_.Name -eq ‘packages’} | Remove-Item –Force –Recurse
I put this in a file named clean-devfolder.ps1 and it works like a champ.
If, instead, you want to see a preview of which directories will be removed, you can add -WhatIf to the end of the whole command.
Even better, you can take this suggestion and integrate it with the Visual Studio IDE’s “Tools” menu by creating an “External Tool”.
To do this, Choose “Tools->External Tools” and then “Add”. Set it up this way:
Title: Power&Wash Solution
Command: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Arguments: -command “get-childitem -Directory -Recurse | where-object {$_.Name -eq ‘bin’ -or $_.Name -eq ‘obj’ -or $_.Name -eq ‘packages’} | Remove-Item –Force –Recurse”
Init Directory: $(SolutionDir)