Removing all unused references from a project in Visual Studio projects

Visual Studio

Visual Studio Problem Overview


I just wondered if it possible within various Visual Studio versions to automatically remove all references from a project that were never been used?

In your answer, please specify which version of VS the solution applies to.

Visual Studio Solutions


Solution 1 - Visual Studio

If you have Resharper (plugin) installed, you can access a feature that allows you to analyze used references via Solution Explorer > (right click) References > Optimize References...

http://www.jetbrains.com/resharper/webhelp/Refactorings__Remove_Unused_References.html

This feature does not correctly handle:

  • Dependency injected assemblies
  • Dynamically loaded assemblies (Assembly.LoadFile)
  • Native code assemblies loaded through interop
  • ActiveX controls (COM interop)
  • Other creative ways of loading assemblies

enter image description here

Solution 2 - Visual Studio

All you need is stone and bare knuckle then you can do it like a caveman.

  1. Remove unused namespaces (for each class)
  2. Run Debug build
  3. Copy your executable and remaining namespace references to new location
  4. Run the executable
  5. Missing Reference DLL error will occur
  6. Copy required DLL from Debug folder
  7. Repeat 4-6
  8. Gu Gu Ga Ga?
  9. Throw your stone

You can also rely on your build tools to let you know which reference is still required. It's the era of VS 2017, caveman still survived.

Solution 3 - Visual Studio

You can try the free VS2010 extension: Reference Assistant by Lardite group. It works perfectly for me. This tool helps to find unused references and allows you to choose which references should be removed.

Solution 4 - Visual Studio

The Resharper extension will do this for you.

This extension supports Visual Studio 2005 through 2017.

While the compiler won't include unused assemblies, extraneous using statements and references slows down Visual Studio and Intellisense, since there's more code the tools have to consider.

Solution 5 - Visual Studio

In a Visual Basic project there is support to remove "Unused References" (Project-->References-->Unused References). In C# there isn´t such a function.

The only way to do it in a C# project (without other tools) is to remove possible unused assemblies, compile the project and verify if any errors occur during compilation. If none errors occur you have removed a unused assembly. (See my post)

If you want to know which project (assembly) depends on other assemblies you can use NDepend.

Solution 6 - Visual Studio

For Visual Studio 2013/2015/2017 there is an extension that does exactly what you want: ResolveUR. What this basically does is:

  • reference is removed in the project
  • project is compiled with msbuild
  • check for build errors
  • restore removed references if there were build errors.

Solution 7 - Visual Studio

With Visual Studio versions 2017 and 2015, you can do this with the Code Map feature, but this feature is only available in the Enterprise Edition, not the Community or Professional versions.

Right-click on the project node in the solution explorer and select 'Show on Code Map.' This will display your .dll as a single node in a blank graph. Right-click on that node in the Code Map and select "Show Assemblies This References." This will add an additional node called "Externals" which can be expanded to show only the assemblies that are actually referenced.

enter image description here

Solution 8 - Visual Studio

For anybody coming here looking for Visual studio 2012:

Download and Install Reference Assistant for Visual Studio 11

Later you can do:

enter image description here

Solution 9 - Visual Studio

In Visual Studio 2013 this extension works: ResolveUR

Solution 10 - Visual Studio

Some people suggested to use an awesome tool - Reference Assistant for Visual Studio. The problem is that VS2012 is the latest supported Visual Studio. But there is the way to make it work in VS2013 as well ;)

And here is how:

  1. Download Lardite.RefAssistant.11.0.vsix

  2. Change the extension to zip: Lardite.RefAssistant.11.0.vsix -> Lardite.RefAssistant.11.0.zip

  3. Unzip and open the extension.vsixmanifest file in the text editor

  4. Find all occurences of InstallationTarget Version="[11.0,12.0)" and replace them with InstallationTarget Version="[11.0,12.0]" (note the closing bracket)

  5. Save the file and zip all files so they are on the root zip level

  6. Change the extension of the new zip to vsix

  7. Install and enjoy :)

I've tested it with VS2013, thanks source for the tutorial

EDIT Add to support VS 2015 Community Edition

<InstallationTarget Version="[14.0,15.0]" Id="Microsoft.VisualStudio.Community" />

Meaning of the brackets

[ – minimum version inclusive.] – maximum version inclusive. 

( – minimum version exclusive. 

) – maximum version exclusive.

Solution 11 - Visual Studio

You can use Reference Assistant extension from the Visual Studio extension gallery.

Used and works for Visual Studio 2010.

Solution 12 - Visual Studio

[Update] This feature is only available for .Net core projects.

This feature will be coming to Visual Studio 2019 very soon and already available with Visual Studio 2019 v16.10 Preview 1.

This option is turned off by default, but you can enable it under menu Tools > Options > Text Editor > C# > Advanced. Select the Remove Unused References command in Solution Explorer (Experimental). Once the option is enabled, the Remove Unused References command will appear in the right-click menu of a project name or dependencies node.

Remove Unused References in Visual Studio 2019 v16.10 Preview 1

Solution 13 - Visual Studio

In the VS2022 (preview at the moment of writing) this comes out of the box for SDK Style Projects (read: .NET Core and newer).

If it is available you can find it in the project context menu: enter image description here

You get to choose what to do with each finding.

Read more about it here.

Pro-tip: Check if your project compiles and runs correctly after applying this. In my experience it doesn't check whether a dependency is used at runtime, for instance.

Solution 14 - Visual Studio

Using DevExpress, I follow these instructions:

  1. In VS, go to DevExpress - Editor - Code Cleanup. Under Rules, check 'Remove unused namespace references'. Click OK.
  2. Right-click on the solution, and choose 'Code Cleanup'. The cleanup runs for a few minutes, and finishes.
  3. Build your application

Solution 15 - Visual Studio

The following method does not depend on any 'add-on's and is not very painful.

Step through each of your source files and

  1. Select all (Ctrl-A)
  2. Toggle outline expansion (Ctrl-M, M). This will reduce the file to two lines.
  3. Click on the namespace's '+'. This will show each of the file's classes as a single line. Scan each class's reference count, looking for unreferenced classes.
  4. Click on each of the classes' '+'. This will show each of the class functions as a single line. Scan each function's reference count, looking for unreferenced functions.

Scanning each file looking for '0 reference' takes only a second.

Scanning an entire project takes only a couple of minutes.

Solution 16 - Visual Studio

In VB2008, it works this way:

Project>Add References

Then click on the Recent tab where you can see list of references used recently. Locate the one you do not want and delet it. Then you close without adding anything.

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
QuestionKroaXView Question on Stackoverflow
Solution 1 - Visual StudioErnestView Answer on Stackoverflow
Solution 2 - Visual StudioEarkView Answer on Stackoverflow
Solution 3 - Visual StudioWackyView Answer on Stackoverflow
Solution 4 - Visual StudioMathiesonView Answer on Stackoverflow
Solution 5 - Visual StudioJehofView Answer on Stackoverflow
Solution 6 - Visual StudioClaudiu ConstantinView Answer on Stackoverflow
Solution 7 - Visual StudioGlenn SlaydenView Answer on Stackoverflow
Solution 8 - Visual StudioHabibView Answer on Stackoverflow
Solution 9 - Visual StudioGeneral FailureView Answer on Stackoverflow
Solution 10 - Visual StudioVladLView Answer on Stackoverflow
Solution 11 - Visual Studioeka808View Answer on Stackoverflow
Solution 12 - Visual StudioDipen ShahView Answer on Stackoverflow
Solution 13 - Visual StudioAageView Answer on Stackoverflow
Solution 14 - Visual StudioIndieTech SolutionsView Answer on Stackoverflow
Solution 15 - Visual StudioNoBrassRingView Answer on Stackoverflow
Solution 16 - Visual StudiocodelearnerView Answer on Stackoverflow