How can I clear the NuGet package cache using the command line?

NugetNuget Package

Nuget Problem Overview


I can clear my development computer's NuGet package cache using Visual Studio menu ToolsOptionsNuGet Package ManagerGeneral: Clear Package Cache button.

I would like to do this on the command line. Unfortunately, I can not find a related command line switch for nuget.exe.

Did I miss something?

Nuget Solutions


Solution 1 - Nuget

First, download the NuGet command line tool from here.

Next, open a command prompt and cd to the directory to which nuget.exe was downloaded.

You can list the local caches with this command:

nuget locals all -list

You can clear all caches with this command:

nuget locals all -clear

Reference: https://docs.nuget.org/consume/command-line-reference

Solution 2 - Nuget

In Visual Studio 2017, go to menu ToolsNuGet Package ManagerPackage Manager Settings. You may find out a button, Clear All NuGet Cache(s):

Enter image description here

If you are using .NET Core, you may clear the cache with this command, which should work as of .NET Core tools 1.0:

dotnet nuget locals all --clear

Solution 3 - Nuget

The nuget.exe utility doesn't have this feature, but seeing that the NuGet cache is simply a folder on your computer, you can delete the files manually. Just add this to your batch file:

del %LOCALAPPDATA%\NuGet\Cache\*.nupkg /q

Solution 4 - Nuget

dotnet nuget locals all --clear

If you're using .NET Core.

Solution 5 - Nuget

For me I had to go in here:

%userprofile%\.nuget\packages

Solution 6 - Nuget

This adds to rm8x's answer.

Download and install the NuGet command line tool.

List all of our locals:

$ nuget locals all -list
http-cache: C:\Users\MyUser\AppData\Local\NuGet\v3-cache
packages-cache: C:\Users\MyUser\AppData\Local\NuGet\Cache
global-packages: C:\Users\MyUser\.nuget\packages\

We can now delete these manually or as rm8x suggests, use nuget locals all -clear.

Solution 7 - Nuget

Note that dnx has a different cache for feeding HTTP results:

Microsoft .NET Development Utility Clr-x86-1.0.0-rc1-16231
   CACHE https://www.nuget.org/api/v2/
   CACHE http://192.168.148.21/api/odata/

Which you can clear with

dnu clear-http-cache

Now we just need to find out what the command will be on the new dotnet CLI tool.

...and here it is:

dotnet restore --no-cache

Solution 8 - Nuget

If you need to clear the NuGet cache for your build server/agent you can find the cache for NuGet packages here:

%windir%/ServiceProfiles/[account under build service runs]\AppData\Local\NuGet\Cache

Example:

C:\Windows\ServiceProfiles\NetworkService\AppData\Local\NuGet\Cache

Solution 9 - Nuget

You can use PowerShell too (same as me).

For example:

rm $env:LOCALAPPDATA\NuGet\Cache\*.nupkg

Or 'quiet' mode (without error messages):

rm $env:LOCALAPPDATA\NuGet\Cache\*.nupkg 2> $null

Solution 10 - Nuget

I ended up here because I was trying to figure out how to delete a specific nuget package from the cache. The answers above talk about deleting all local packages using nuget locals all -clear.

In case anyone is interested in clearing a specific package, I'm sharing my findings:

  • You can't currently clear a specific package using the nuget or dotnet cli. There is a github issue open to fix this: https://github.com/NuGet/Home/issues/5713
  • For now, you have to resort to filesystem commands (as some of the answers above mention) to delete the specific package. For mac/linux, it was rm -rf ~/.nuget/packages/<package-name>
  • What I actually needed was a way to delete the cache of a specific package that I was about to re-publish to a local nuget repository (using a fixed version number to make local development easier). I accomplished this by adding the following to the .csproj file of the nuget-packaged library:
    <Project>
      <Target Name="DeleteLocalCache" BeforeTargets="Pack">
        <RemoveDir Directories="$(NugetPackageRoot)/$(PackageId.ToLower())/1.0.0-local"/>
      </Target>
    </Project>

Solution 11 - Nuget

For those who installed nuget on RHEL7 (e.g., from the EPEL repository) using sudo yum install nuget, here is where local cache is located:

~/.local/share/NuGet/Cache

Solution 12 - Nuget

I've saw my nugets in this folder (on build server): c:\Windows\System32\config\systemprofile.nuget\packages\

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiong.pickardouView Question on Stackoverflow
Solution 1 - Nugetrm8xView Answer on Stackoverflow
Solution 2 - NugetRickyView Answer on Stackoverflow
Solution 3 - NugetKilimanView Answer on Stackoverflow
Solution 4 - NugetJim AhoView Answer on Stackoverflow
Solution 5 - NugetBowofolaView Answer on Stackoverflow
Solution 6 - NugetShaun LuttinView Answer on Stackoverflow
Solution 7 - NugetKCDView Answer on Stackoverflow
Solution 8 - NugetkhablanderView Answer on Stackoverflow
Solution 9 - Nugethonzakuzel1989View Answer on Stackoverflow
Solution 10 - NugetGeorge MView Answer on Stackoverflow
Solution 11 - NugetdokasparView Answer on Stackoverflow
Solution 12 - NugetAndrey RavkovView Answer on Stackoverflow