Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear a specific NuGet package from local cache using command line?

Question

Is it possible to clear a specific NuGet package from NuGet local cache using command line?

Context

I would like to ensure when I am executing a package restore operation for a project, the very latest server binaries will be restored, not something from the cache. At the same time I do not want to delete all packages from the cache.

like image 919
g.pickardou Avatar asked May 13 '16 13:05

g.pickardou


2 Answers

If you have NuGet.exe version 3.3 or higher you can clear the entire cache from the command line:

nuget locals <all | http-cache | packages-cache | global-packages> -clear

If your NuGet packages are downloaded to the NuGet v2 cache then you could just have a simple script to delete a particular .nupkg file from the packages cache.

del %LocalAppData%\NuGet\Cache\NUnit.2.6.4.nupkg

With the NuGet v3 cache the structure is a bit different so you would need to do remove a set of directories:

rmdir %UserProfile%\.nuget\packages\NUnit\2.6.4 /s
like image 178
Matt Ward Avatar answered Oct 07 '22 06:10

Matt Ward


  • You can't currently delete a specific package from the cache using the nuget or dotnet cli. There is a github issue to address that: https://github.com/NuGet/Home/issues/5713
  • As Matt Ward's answer states, you are left with having to use filesystem commands to delete a package from the cache. With nuget V3+, on mac/linux, the command is:
rm -rf ~/.nuget/packages/<package-name>
  • When I was trying to delete a specific package from the cache, what I really wanted was to do so before I re-published that package to a local nuget repository (I use a fixed version number for the local repository to make local development of nuget packages 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>
like image 23
George M Avatar answered Oct 07 '22 04:10

George M