599

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
g.pickardou
  • 32,346
  • 36
  • 123
  • 268

12 Answers12

882

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

Mark Whitaker
  • 8,465
  • 8
  • 44
  • 68
rm8x
  • 9,135
  • 1
  • 13
  • 18
  • 12
    Works nicely for 3.3 but only for the current user - I had an issue with a corrupt local cache on our build server which was (sadly) running under Local System, so the cache wasn't listed - actual location was `C:\Windows\SysWOW64\config\systemprofile\AppData\Local\NuGet\Cache\` – Zhaph - Ben Duguid Mar 08 '16 at 12:11
  • 3
    Maybe you should run the clear operation under the very same user as the build operation. Either with configuring a dedicated build user, or using a trick to run the clear under Local System. – g.pickardou Apr 26 '16 at 05:46
  • 6
    Is there any possibilities of removing particular NuGet from cache? for ex: I want to remove NuGet X from cache and am not aware of NuGet cache location, in this situation how to remove "X" alone from cache – user3610920 Jul 13 '16 at 09:41
  • 28
    I had to run `nuget update -self` to update the _nuget.exe_ I downloaded from this link otherwise I got the error `Unknown commmand: 'locals'` – ajbeaven Sep 11 '16 at 23:30
  • 1
    If you have VS 2017, don't do this. See https://stackoverflow.com/a/42665980/1754995 (answer by Ricky below). – Kody Nov 01 '17 at 17:20
  • 4
    make sure you close your VS since `devenv.exe` might be blocking some package folders and they won't be cleared. – Sharif Nov 08 '17 at 15:02
  • 2
    Be careful with this command. It will cause a lot of downloads and CPU usage in Visual Studio later. Using `dotnet nuget locals http-cache --clear` seems more reasonable option. It will provide less disk space though. – Roland Pihlakas Jul 03 '21 at 18:58
  • I just cleaned out 34 GBs of data. – James John McGuire 'Jahmic' Mar 05 '23 at 08:53
363

In Visual Studio 2022, go to menu ToolsNuGet Package ManagerPackage Manager Settings. You may find out a button, Clear All NuGet Storage:

Button to clear NuGet package cache in Visual Studio 2022

The steps are similar in previous versions of Visual Studio too.

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
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Ricky
  • 10,044
  • 3
  • 26
  • 31
96

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kiliman
  • 18,460
  • 3
  • 39
  • 38
  • 7
    At the time of asking this was the only solution, so was the best answer. Now I've unmarked it, and credited @rmoore's answer after trying it out. – g.pickardou Apr 20 '16 at 04:34
  • 1
    I needed to clear the cache for a different user (the build identity), and this was the only mechanism that cleared the cache. The command line tool would only clear the caches for my own identity. – MarkPflug Jul 08 '20 at 18:13
87

dotnet nuget locals all --clear

If you're using .NET Core.

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
42

For me I had to go in here:

%userprofile%\.nuget\packages
Bowofola
  • 1,132
  • 9
  • 12
23

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • 1
    what is the point of deleting if nuget will generate all those with every build? why nuget is creating so many folders indeed? – Emil Nov 17 '16 at 10:42
  • 3
    @batmaci because over time packages get updated and you'll end up with `Batmaci 1.0`, `Batmaci 1.1`, `Batmaci 1.2` where all you need is the latest one – Simon_Weaver May 25 '17 at 00:31
  • 1
    We had the occasional problem on our build server with not finding NuGet packages in the cache. We solved it by always clearing the cache at the start of the build. – Steve Wright Apr 27 '20 at 20:13
19

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KCD
  • 9,873
  • 5
  • 66
  • 75
  • 2
    I would go with the approach above from Ruslan as I has some issues with packages even passing --no-cache – Alexz S. Jan 09 '17 at 15:32
  • 1
    Running `dotnet restore --no-cache` worked for me, but I had to do it from a Powershell prompt running as administrator – shanabus Feb 28 '19 at 20:48
  • this command helped me work out what was going on when `dotnet run` was failing to restore packages but not providing any useful output – JonnyRaa Aug 14 '23 at 11:19
9

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
khablander
  • 159
  • 1
  • 5
  • 3
    I took the liberty of marking the path to make it more readable, and replacing [windows dir] with `%windir%`, which will automatically put you in the right directory when entered into e.g. windows explorer. – Kjartan May 12 '16 at 07:14
5

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
honzakuzel1989
  • 2,400
  • 2
  • 29
  • 32
4

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>
George M
  • 81
  • 4
0

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
dokaspar
  • 8,186
  • 14
  • 70
  • 98
0

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

Andrey Ravkov
  • 1,268
  • 12
  • 21