ChatGPT解决这个技术问题 Extra ChatGPT

How do I delete NuGet packages that are not referenced by any project in my solution?

Somehow during the upgrade to VS2012 and .NET 4.5, I've managed to get NuGet confused. There are packages that appear in the package manager (and the packages folder) that I cannot delete (I believe they are legacy ASP.NET NuGet packages that have been replaced with new package names with the new version. They each have only a Manage button -- but no uninstall button. And when you look to see where they are used, you can see that they are referenced by none of the solution's projects? They are NOT displaying an Uninstall?


J
James L

I've found a workaround for this.

Enable package restore and automatic checking (Options / Package Manager / General) Delete entire contents of the packages folder (to Recycle Bin if you're nervous!) Manage Nuget Packages For Solution Click the restore button.

NuGet will restore only the packages used in your solution. You end up with a nice, streamlined set of packages.


Won't this not remove the package from packages.config and not remove the references from the project?
@CodeCaster hard to parse your double negative. Nothing will be removed from config and references. The idea here is to remove the NuGet packages that are no longer referenced
Excellent answer! If you are using TFS you should delete all content inside of ./packages in source countrol explorer first so that other clients like build machines will remove redundant items too.
@sprinter252, if you're using package restore, you're not supposed to check the packages into source control in the first place. That's one of the main benefits of using package restore. As part of converting to package restore, an 'ignore' rule should be placed on the packages folder.
@CarlBussema You're right. Since the new NuGet-version this isn't so important because Nuget won't add any binaries into the subfolders. So you're only getting the folder structure. My comment was related to the default behavior of TFS (packages-folder included). On TFS Anywhere ignores are ignored often ;-). Important for mixed projects with Xamarin.
P
Pavel Bakshy

You can use Package Manager Console with command: Uninstall-Package PackageId to remove it, or just delete package folder from 'packages' folder under solution folder. More information about Package Manager Console you can find here: http://docs.nuget.org/docs/reference/package-manager-console-powershell-reference


Thanks Pavel. I had a feeling you could just delete the folders. It's a little disconcerting because I have to manually ensure that none of the packages are referenced (they don't appear to be). I'm still kind of curious to know why the package manager doesn't show an Uninstall (or remove) button.
Package manager on solution level displays all packages references to projects from solution and additional all packages which placed in packages folder, but don't referenced. For this packages no uninstall ability in UI. I'm agree that it's result of incorrect behavior, but in this case you should check it manually. And delete package or reinstall this when you will be sure need you it or not.
By just removing the folder and going back to NuGet and see the package, it somehow still displays as installed with the green tick beside it, better to do the proper uninstallation using the console.
@ysrb, its a powershell command which goes verb-noun such as get-user or remove-item, your example is noun-verb
@ysrb As Nick Young says, and besides, it would be a horrendous compliment to Install-Package.
A
Abrar Jahin

First open the Package Manager Console. Then select your project from the dropdown list. And run the following commands for uninstalling nuget packages.

Get-Package

for getting all the package you have installed.

and then

Uninstall-Package PagedList.Mvc

--- to uninstall a package named PagedList.MVC

Message

PM> Uninstall-Package PagedList.Mvc
Successfully removed 'PagedList.Mvc 4.5.0.0' from MCEMRBPP.PIR.

This answer worked perfectly for me. My situation was that the package only showed up under the Updates tab of the dialog... and when I hit Update no projects had the package installed -- so there was no way to update it or remove it.
佚名

If you want to delete/uninstall Nuget package which is applied to multiple projects in your solutions then go to:

Tools-> Nuget Package Manager -> Manage Nuget Packages for Solution In the left column where is 'Installed packages' select 'All', so you'll see a list of installed packages and Manage button across them. Select Manage button and you'll get a pop out, deselect the checkbox across project name and Ok it

The rest of the work Package Manager will do it for you.


This is definitely the answer i was looking for, thanks!
Z
Zach Ioannou

VS2019 > Tools > Options > Nuget Package Manager > General > Click on "Clear All Nuger Cache(s)"


Thank you. This resolved my issue. The references were gone. But they still showed in the Solution Explore.
C
Community

If you have removed package using Uninstall-Package utility and deleted the desired package from package directory under solution (and you are still getting error), just open up the *.csproj file in code editor and remove the tag manually. Like for instance, I wanted to get rid of Nuget package Xamarin.Forms.Alias and I removed these lines from *.csproj file.

https://i.stack.imgur.com/MhfyH.png

And finally, don't forget to reload your project once prompted in Visual Studio (after changing project file). I tried it on Visual Studio 2015, but it should work on Visual Studio 2010 and onward too.

Hope this helps.


D
Dov Miller

An alternative, is install the unused package you want to delete in any project of your solution, after that, uninstall it and Nuget will remove it too.

A proper uninstaller is needed here.


Is there a way to get it to uninstall properly without reinstalling it?
i
irfandar

Solution 1

Use the powershell pipeline to get packages and remove in single statement like this

Get-Package | Uninstall-Package

Solution 2

if you want to uninstall selected packages follow these steps

Use GetPackages to get the list of packages Download Nimble text software Copy the output of GetPackages in NimbleText(For each row in the list window) Set Column Seperator to ( if required Type Uninstall-Package $0 (Substitute using pattern window) Copy the results and paste them in Package Manage Console

That be all folks.


Can you explain more about Solution 2? i'm not getting it right
Which step in solution 2
C
CarenRose

One NuGet package can reference another NuGet package. So, please be very careful about inter-package dependencies. I just uninstalled a Google map package and it subsequently uninstalled underlying packages like Newtonsoft, Entity Framework, etc.

So, manually deleting particular package from packages folder would be safer.


J
Jazimov

From the Package Manager console window, often whatever command you used to install a package can be used to uninstall that package. Simply replace the INSTALL command with UNINSTALL.

For example, to install PowerTCPTelnet, the command is:

Install-Package PowerTCPTelnet -Version 4.4.9

To uninstall same, the command is:

Uninstall-Package PowerTCPTelnet -Version 4.4.9


L
Lance U. Matthews

If you want to use Visual Studio option, please see How to remove Nuget Packages from Existing Visual Studio solution:

Step 1: In Visual Studio, Go to Tools/NuGet Package Manager/Manage NuGet Packages for Solution…

Step 2: UnCheck your project(s) from Current solution

Step 3: Unselect project(s) and press OK


It is preferable to write out the answer in StackOverflow rather than just posting a link to it because links can expire in the future.