If you work with .NET long enough, you will eventually run into a baffling build error where your code looks completely correct, but the compiler stubbornly refuses to cooperate. Often, the culprit isn't your code—it's stale build artifacts or corrupted package caches.
In this quick guide, we’ll look at two powerful commands every .NET developer should have in their toolkit: dotnet clean and dotnet nuget locals all --clear.
What does dotnet clean do?
When you build a .NET project, the compiler generates temporary files, compiled binaries, and cache files stored inside the bin/ and obj/ directories of your project folder.
The dotnet clean command deletes these build outputs (the contents of the bin and obj folders).
Why use it?
To force a completely fresh build from scratch.
To resolve issues where old compiled files aren't being updated properly during incremental builds.
Before packaging or publishing your application to ensure no old artifacts sneak in.
Bash
dotnet clean
(Note: This only affects the local project directory and does not delete your source code.)
What does dotnet nuget locals all --clear do?
While dotnet clean clears local project build outputs, NuGet handles external library dependencies. .NET stores downloaded NuGet packages in global machine caches (such as http-cache, global-packages, and temp) so it doesn't have to re-download them every time.
The dotnet nuget locals all --clear command completely wipes out all local NuGet caches across your machine.
Why use it?
To fix stubborn package restore errors (e.g., NU1101, NU1202).
When a package download was interrupted or corrupted, causing constant build failures.
To force .NET to fetch fresh copies of packages from NuGet.org (useful when working with prerelease packages or packages with the same version number updated locally).
Bash
dotnet nuget locals all --clear
When should you use them together?
If you are experiencing mysterious build errors that won't go away after a standard rebuild, try running them in sequence as a "nuclear option" to reset your local environment:
# 1. Clean local build artifacts
dotnet clean
# 2. Clear all global NuGet caches
dotnet nuget locals all --clear
# 3. Restore dependencies and rebuild fresh
dotnet restore
dotnet build