How can you find unused NuGet packages in solution?

How can you find the unused NuGet packages in a solution?

I've got a number of solutions where there are a lot of installed packages, and a large number of them are flagged as having updates.

However, I'm concerned there may be breaking changes, so I first want to clean up by removing any unused packages.


Solution 1:

ReSharper 2016.1 has a feature to remove unused NuGet.

It can be run on a solution and on each project in a solution and it does the following things:

  1. Analyze your code and collecting references to assemblies.
  2. Build NuGet usage graph based on usages of assemblies.
  3. Packages without content files, unused itself and without used dependencies are assumed as unused and suggested to remove.

Unfortunately, this doesn't work for project.json projects (RSRP-454515) and ASP.NET core projects (RSRP-459076)

Solution 2:

You can use the Visual Studio Extension ResolveUR - Resolve Unused References.

Resolve unused references including nuget references in Visual Studio 2012/2013/2015 projects via menu item at solution and project nodes Solution Explorer Tool Window

It's not an easy task, so I suggest to make a backup and/or commit before, just in order to rollback if something went wrong.

Solution 3:

You can accomplish this using ReSharper 2019.1.1.

Right click on the project > Refactor > Remove Unused References.

If your project is small, you can also use: project > Optimize Used References . . .

A window will pop up. Select all references and remove them all. Then go back and re-add the ones that give you a compiler error.