Understanding & Managing Nuget Cache Folders
[.NET, Nuget]
In the context of NuGet packages, what happens when you run dotnet add package
?
It depends.
If the package that you are adding has been added to another project on your machine, the package will be retrieved from a cache and then added as a reference to your project.
If it’s a package you are installing for the first time, it will be downloaded from whichever source (probably nuget) and then added as a reference to your project.
Have you ever wondered where these caches are, and how you can view their contents?
The caches are just normal directories on your machine, and can be viewed by running the following command:
dotnet nuget locals all --list
On my MacBook running macOS, it prints the following:
http-cache: /Users/rad/.local/share/NuGet/http-cache
global-packages: /Users/rad/.nuget/packages/
temp: /var/folders/q8/cdslzt2s6p1djnhp_y3ksc280000gn/T/NuGetScratch
plugins-cache: /Users/rad/.local/share/NuGet/plugin-cache
As you can see, there are https://en.wikipedia.org/wiki/MacOS main folders:
- Global packages cache
- Http cache
- Temp cache
- Plugins cache
Global Packages Cache
This is the location where the actual nuget packages (.nupkg) are stored after download.
Each package is stored in a folder with the corresponding name.
You can view the contents directly by running the following command:
ls /Users/rad/.nuget/packages/
This returns the following: a list of folders:
If you view one of the folders, it will have this structure:
The structure consists of the following:
The top level is the version of the package, into which goes the following:
- .nupkg itself
- A hash of the package
- A nuspec file
- A logo (if provided)
- A readme (if provided)
- A lib folder
The lib folder contains the actual DLLs themselves, packaged into folders according to the target.
HTTP Cache
This is the location where nuget maintains some metadata about the downloaded packages.
You can view it using the following command:
ls -l /Users/rad/.local/share/NuGet/http-cache
In this folder, you will find a generated folder that you can then expand.
It contains a number of files with the .dat
extension.
These .dat
files are JSON
files that you can view directly.
To view the first one, I can run:
cat list_awesomeassertions.dat
To pretty-print it, you can pipe this to the jq utility:
Plugin Cache
This folder is used to store the following:
- Invocation results
- Executable paths
- Credential tokens (for authenticated sources)
Temp Cache
This cache stores intermediate or temporary files during operations like restore, install and pack.
Over time, these caches can grow significantly, and you might want to clear them.
This is generally not an issue, as they will be repopulated whenever you perform a package installation or restore. However, you may run into problems if one (or more) of your nuget sources is unavailable.
There are two ways to clear them:
- Manually (by deleting the contents of the folders)
- Using the NuGet tools for this purpose
The following commands are available:
To clear the global cache:
dotnet nuget locals global-packages --clear
To clear the HTTP cache:
dotnet nuget locals http-cache --clear
To clear the temp cache:
dotnet nuget locals temp --clear
There isn’t a dotnet
command to clear the plugins cache directly.
To clear all the caches (including plugins):
dotnet nuget locals all --clear
TLDR
.NET utilizes several caches to enhance the performance of package installation. It also has tools to aid in the management of these caches.
Happy hacking!