NuGet Cache Clearing

Like any package manager NuGet sometimes requires you to clear your cache of packages. For whatever reason sometimes the cache simply seems to get “corrupted” or at least certain packages do. In the past it has been extremely difficult to track and follow the various cache locations of NuGet packages depending on your version and OS. Now Microsoft has a single article describing the locations on each platform for the different types of caches: Managing the global packages, cache, and temp folders

Clear All Caches

I can easily clear the cache by navigating to the relevant folders and deleting them on the file system. But honestly, I’d rather it be a bit easier from the command line. Back when the NuGet CLI was in mainstream you could easily ask it for the cache locations and even clear them:

// list all cache locations
nuget locals all -list

// clear all cache locations
nuget locals all -clear

Using dotnet CLI for pretty much all development now with .NET, and the fact that it restores packages, has me with little reason to maintain or keep the original NuGet CLI directly accessible on my path, and for quite some time I was a bit too lazy to fix that.

Introduced in .NET Core 2.x SDK (and I didn’t realize that until recently), is the NuGet CLI integrated into the dotnet CLI. NICE! Now we can easily get the information we need, and clear the cache as before:

// list all cache locations
dotnet nuget locals all --list

// clear all cache locations
dotnet nuget locals all --clear

Woohoo… all good right?

Clearing the HTTP Cache

If your intention was to clear all the caches, then you are good. But often as a package creator I find myself specifically having issues with my HTTP Cache. Specifically, I may believe a new version of my package is available in the feed (private or public) and attempt to install it into a testing project or consume it somehow… a bit too early… I do get impatient sometimes. At this point, NuGet will detect that the package version does not exist and cache that as a result. 10 seconds later the package is available in the feed, but my local HTTP Cache for NuGet is now holding that 404 result for at least 30 min.

I can clear this up by manually deleting the HTTP cache folders on windows here:

%localappdata%\NuGet\v3-cache 

Or I can also use the dotnet / NuGet CLI to clear specific caches:

dotnet nuget locals http-cache --clear