Skip to main content

Clearing NuGet Package Caches

Development
development
/dotnet
/nuget
2 min read

How to Clear Your NuGet Caches

NuGet caches downloaded packages to avoid lengthy update times. While this is generally helpful, it can sometimes lead to issues, especially when restoring packages in environments like CI servers. Below is a guide to clearing your NuGet caches to help debug and resolve restore problems.

Why Clear NuGet Caches?

Clearing NuGet caches can resolve issues such as:

  • Corrupted or outdated package files.
  • Conflicts between different versions of the same package.
  • Problems with package restore in CI/CD pipelines.

Manually Clearing Folders

To manually clear NuGet caches, remove the following folders:

- Packages Folder (In Project)
- %userprofile%\.nuget\packages
- %localappdata%\NuGet\Cache
- %localappdata%\NuGet\v3-cache
- %localappdata%\NuGet\plugins-cache

Using the CLI

The NuGet CLI and .NET CLI provide commands to clear specific caches or all caches at once. Here’s how to use them:

# Clear the 3.x+ cache (use either command)
dotnet nuget locals http-cache --clear
nuget locals http-cache -clear

# Clear the 2.x cache (NuGet CLI 3.5 and earlier only)
nuget locals packages-cache -clear

# Clear the global packages folder (use either command)
dotnet nuget locals global-packages --clear
nuget locals global-packages -clear

# Clear the temporary cache (use either command)
dotnet nuget locals temp --clear
nuget locals temp -clear

# Clear the plugins cache (use either command)
dotnet nuget locals plugins-cache --clear
nuget locals plugins-cache -clear

# Clear all caches (use either command)
dotnet nuget locals all --clear
nuget locals all -clear

Pro Tips

  1. Automate Cache Clearing: Use scripts to automate cache clearing in CI/CD pipelines to avoid manual intervention.
  2. Verify Cache Clearing: After clearing caches, run dotnet restore or nuget restore to ensure packages are restored correctly.
  3. Use with Caution: Clearing caches can lead to longer restore times as packages are re-downloaded. Use this method only when necessary.

Final Thoughts

Clearing NuGet caches is a simple yet effective way to resolve package-related issues. Whether you’re debugging a local project or troubleshooting a CI/CD pipeline, these steps can save you time and frustration. Remember to use these commands judiciously to maintain efficient workflows.