Recursively remove bin/obj folders Powershell

To really "clean" that solution/project

Posted by admin on April 24, 2024

Update: July 28, 2025

Here is a better command I found for removing only those bin and obj folders.

Get-Childitem -Path . -Include bin,obj -Recurse -Directory | Where-Object { $_.FullName -notmatch '\\(node_modules|packages)\\' } | Remove-Item -Recurse -Force

Here is breakdown of the command.

Get-ChildItem -Path . is used to get the file and directory objects in the current path. Optionally, you can change the argument passed to -Path to which ever path you like. If your projects folders are solely in the ./src directory you can use Get-ChildItem -Path ./src instead to narrow your directory traversal.

-Include bin,obj filters the child items to just the bin and obj folders. If you want to include different directories you can change the argument passed to -Include.

-Recurse -Directory recursively goes through down through all the directories.

Note that this above command is three separate commands piped together. The first command Get-Childitem -Path . -Include bin,obj -Recurse -Directory is then piped to Where-Object { $_.FullName -notmatch '\\(node_modules|packages)\\' }.

Here is a breakdown of the Where-Object { $_.FullName -notmatch '\\(node_modules|packages)\\' } command.

Where-Object { ... } is an additional filter on all the directories that the first command is recursively navigating through. Everything inside the curly braces { } is how it filters.

$_.FullName -notmatch '...' is doing a regular expression operation where -notmatch means that any directory paths that do NOT match the filter string (the thing between the single quotes) will be include. At a high level, this whole command is how you can exclude certain directories from being considered when deleting the bin and obj folders. In particular, the node_modules directory is ecluded because that is related to gulp operations and the NPM packages will download packages that have bin and obj folders. This is turn disrupts the gulp tooling which is not desired. Additionally, the packages folder is also excluded because that is sometimes used by nuget to store downloaded packages. I skip it here for the same reasons as I do for node_modules.

Essentially the entire second command Where-Object { $_.FullName -notmatch '\\(node_modules|packages)\\' } is about skipping those directories that have a bin and obj folder I want to keep before sending to the 3rd and final command Remove-Item -Recurse -Force.

The final command Remove-Item -Recurse -Force is what does the deleting of the directory.

The Remove-Item is the PowerShell command. The -Recurse will recursively delete the child items like files and additional folders. The -Force option deletes the directory without requiring you to confirm you want to delete a non-empty directory.

That's it. That's the command that I use to recursively remove bin and obj folders. Use this rather than the implementation below, which is there for reference.

Original Implementation (DO NOT USE, kept here for legacy reasons)

Run these commands to remove all the bin and obj folders.

get-childitem $path -include bin -recurse | remove-item
get-childitem $path -include obj -recurse | remove-item

Special thanks to this post for giving me the answer

Why?

Sometimes in the .NET solution, even the batch build's clean and rebuild commands do not clear out all files from the bin and obj folders. Reason being is the msbuild will not remove files it is not aware.

The exact scenario I need to solve was that I had changed the version number of a nuget library but when I rebuilt the solution, the old assembly file was in the bin folder and that was used rather that you new downgraded version.

More precisely in my case, I had repackages a new version of a local nuget package using the same exact version number. Because...reasons.

Conclusion

When it doubt, clean it out.

If you found value in this post, consider following me on X @davidpuplava for more valuable information about Game Dev, OrchardCore, C#/.NET and other topics.