Recursively delete .NET bin and obj folders
- 3 minutes read - 516 wordsTips & tricks …
… and sometimes “things I always have to look up how to do”
The obj
and bin
folders in a .NET solution are created by the compiler as part of the compilation/build process.
They contain intermediate object files and the final binary outputs, respectively.
However, sometimes things go a bit strange, and you get odd build failures. Perhaps after chopping and changing NuGet package versions or switching Git branches. Sometimes you build up a lot of old binaries, especially if your solution targets multiple platforms and/or .NET versions.
At this point you would naturally reach for the Clean solution...
command in your IDE or dotnet clean
at a prompt.
And you could well be disappointed as neither properly cleans the obj
and bin
folders - stuff is always left behind.
Now this might be ok for your strange build failure (or it might not) but it really doesn’t do the job if you want to truly
get purge these files/folders.
So here are quick Powershell, Bash and CMD one-liners to recursively purge these folders, starting from the current folder (with some helpful ChatGPT explanations):
Powershell:
Get-ChildItem . -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }
Get-ChildItem . -include bin,obj -Recurse
:Get-ChildItem .
: Retrieves all items (files and directories) in the current directory (.
).-include bin,obj
: Filters the results to include only items namedbin
orobj
.-Recurse
: Searches recursively through all subdirectories.
foreach ($_)
:- This loops through each item found by the
Get-ChildItem
command. In PowerShell,$_
represents the current item in the pipeline (eachbin
orobj
directory found).
- This loops through each item found by the
remove-item $_.fullname -Force -Recurse
:remove-item
: Deletes the item (file or directory).$_
: The current object in the loop, representing the directory being processed..fullname
: The full path to the item being deleted.-Force
: Forces the deletion of the item, bypassing any restrictions (like read-only files).-Recurse
: Ensures that all contents of the directory (including subdirectories and files) are deleted.
Bash:
find . -type d \( -name "bin" -o -name "obj" \) -exec rm -rf {} +
find .
starts searching from the current directory (.
).-type d
restricts the search to directories.\( -name "bin" -o -name "obj" \)
specifies that it should look for directories namedbin
orobj
.-exec rm -rf {}
will forcefully and recursively remove the found directories.+
tellsfind
to pass all matching files to a single rm command for efficiency.
CMD:
for /d /r . %d in (bin,obj) do @if exist "%d" rd /s/q "%d"
for /r %%i in (.)
loops through all directories and subdirectories starting from the current one.if exist "%%i\bin"
checks if thebin
directory exists and then deletes it usingrmdir /s /q
, where/s
removes all contents and/q
suppresses confirmation prompts.- The same check and delete is done for the
obj
directory.
References
I’ve had these lying around in a utils
folder for years so I’m not sure of the exact source. However, re-Googling whist
writing this post turns up this SO post,
so there is a good chance that was my starting point.