306

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.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Mohnkuchenzentrale
  • 5,745
  • 4
  • 30
  • 41
  • 4
    Generally wondering, Does removing these DLLs result in observable improvement in build time or something else? (Of course, other than removing redundant dependencies) – monish001 Dec 24 '14 at 06:32
  • 3
    is there any similar free extension for VS2015? – George Birbilis Nov 17 '15 at 12:52
  • 1
    VB.Net project have it since VS 2005, but it was never implemented for C# projects https://msdn.microsoft.com/en-us/library/7sfxafba – Slai Jan 09 '17 at 12:30
  • 1
    Try reshrper { References-> remove unused references} –  Feb 01 '17 at 14:33
  • 1
    Resharper has excellent feature, Right Click on References in Solution Explorer and then "Remove unused References" will be visible, keep in mind, it can only detect compile time references, anything used and bound at run time will be removed also – Noman Khan Apr 21 '17 at 19:54
  • 4
    You can [vote](https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/2280976-remove-unused-references) to have this feature included as part of VS (as it should be). – Mike Lowery Aug 30 '17 at 16:49
  • 2
    @monish001 - every assembly and asset increases the deployment footprint and is another thing to manage/maintain or be a potential source for bugs. – StingyJack Aug 23 '18 at 12:38

17 Answers17

60

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

Ed Greaves
  • 4,807
  • 2
  • 22
  • 19
Ernest
  • 1,370
  • 13
  • 23
  • 1
    Your last statement "However it's not perfect, any assemblies which are dependent on referenced assemblies in your current project are still marked as unused." is actually not a problem. The build knows to copy the references of your references. See this answer: http://stackoverflow.com/a/2290139/26262 – Ed Greaves Apr 28 '16 at 15:26
  • 1
    While this is a viable way to do this the "within various Visual Studio versions" to me means not something else but already "within" – Mark Schultheiss Mar 06 '20 at 18:34
36

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.

Eark
  • 485
  • 4
  • 5
  • 4
    Haha, well, nothing wrong with a little humor here. Stoneman approach indeed, looks like without spendy Enterprise edition its what we've got right now. – Nicholas Petersen May 13 '19 at 20:22
28

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.

Nick N.
  • 12,902
  • 7
  • 57
  • 75
Mathieson
  • 1,698
  • 2
  • 17
  • 19
  • 15
    that one have found a lot of unused references in out project - but a bunch of them where really in use... So i do not trust Resharper with references – Offler Mar 27 '14 at 10:34
  • 8
    Resharper consumes tons of memory in big projects when there are many references which eventually crashes VS – Cemre Mengü Apr 16 '14 at 21:01
  • 2
    @eraj VS Express editions do not support extensions, get the free Community edition, that supports extensions like ReSharper. – codemonkeh Jun 23 '15 at 05:14
  • -1 for Resharper. I like Reshaper for code support but it took me a 1/2 hour to restore all of the dependencies that Resharper didn't think I needed. Luckily I took a screenshot before trying it. – Pete Aug 14 '15 at 00:47
  • 4
    Pete, any automated tool will have the possibility of overreaching. Especially in the age of DI and PCL dependencies which appear to be ununsed might be needed, but that's hardly the fault of the tool. Rather than doing a screenshot, you should be using version control software. A modern system like Git or Hg will let you checkin frequently locally, so you don't ever lose work. – Mathieson Aug 28 '15 at 14:45
  • ReSharper supports VS 2015 now. – AJ Richardson Nov 15 '15 at 15:12
  • 2
    as far as i can tell tell Resharper shows unused namespaces, not assembly references which is what I think the question "unused references from a project" means. I'd like to find the latter as well – Sonic Soul Mar 07 '16 at 21:36
  • 2
    @SonicSoul See: Remove unused references: https://www.jetbrains.com/help/resharper/2016.2/Refactorings__Remove_Unused_References.html and Optimise references: https://www.jetbrains.com/help/resharper/2016.2/Navigation_and_Search__Finding_Usages__Optimizing_References.html – bytedev Oct 27 '16 at 11:25
  • But why does it not also remove references in XAML files ? I have to open XAML files and do manually Strg+R, STRG+G. To sort and remove unused references. – Devid Apr 14 '20 at 10:49
28

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.

David Bond
  • 149
  • 1
  • 9
Wacky
  • 459
  • 5
  • 4
22

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.

Community
  • 1
  • 1
Jehof
  • 34,674
  • 10
  • 123
  • 155
22

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.
Claudiu Constantin
  • 2,138
  • 27
  • 38
21

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

S1r-Lanzelot
  • 2,206
  • 3
  • 31
  • 45
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
  • 2
    If you have Enterprise edition and do not see this option in the context menu, then you need to install the "Code Map" feature from the VS installer under the Individual Components panel. – StingyJack Aug 23 '18 at 14:08
  • This is a good one, Thank you @Glenn. I just want to say that I've had Micosoft.Office.Interop.Excel in use and it wasn't listed among the other references, so still have to be a bit carefull. – Emil Filip Aug 02 '19 at 15:59
16

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

Habib
  • 219,104
  • 29
  • 407
  • 436
14

In Visual Studio 2013 this extension works: ResolveUR

itzmebibin
  • 9,199
  • 8
  • 48
  • 62
General Failure
  • 141
  • 1
  • 4
  • 17
    @abarisone Your generic "don't use a link as an answer" response really should only be used AFTER you have actually LOOKED at the answer. The link is the only thing that IS relevant for this answer. It's a link to a VS plugin, so there is nothing else relevant to include. – Charles Boyung Oct 19 '16 at 19:59
  • I agree with Charles. This seems like moderation abuse from @abarisone. This answer really helped me save some time and I would've hated it if any mods abused their powers to delete it. – Zun Dec 12 '19 at 10:42
  • @Zun First of all I've never been a moderator at all or claimed to be. My intent was to ask for more information about the extension. It may have been superficial by my side, but I surely didn't meant to commit an abuse. – abarisone Dec 12 '19 at 10:50
12

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.
VladL
  • 12,769
  • 10
  • 63
  • 83
  • 1
    This works just fine! Seems like a simple typo then which could have been fixed years ago by the author - but he seems to have disappeared, at least the website has... – wexman Jun 18 '15 at 15:51
  • It's not a typo, but the highest supported VS version :). The developer is supposed to test his add-on with every new VS version and increment the number, but for some reason they didn't do that. – VladL Jun 18 '15 at 18:12
  • Remember to stop "copy and pasting" stuff. :) – Rikki Sep 18 '15 at 16:49
  • @RikkiRockett Are you addressing me? I've linked the source in my answer. Or what exactly do you mean? – VladL Sep 18 '15 at 16:59
  • 2
    Nah man. I was joking. Just saying to the dude who put ")" instead of "]" who made us all go through your instructions. :) Nice job though, just did what you said and all fine! You just saved me a day of struggling with this. (y) – Rikki Sep 18 '15 at 17:19
  • @ch.smrutiranjanparida pretty sure yes, try Version="[9.0,10.0]" – VladL Nov 24 '16 at 10:04
11

[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

Dipen Shah
  • 25,562
  • 1
  • 32
  • 58
  • 1
    Is this available only for .NET Core projects? – Panu Oksala May 26 '21 at 06:04
  • 1
    I have the setting enabled, but still unable to see the button for .NET framework projects. – Panu Oksala May 26 '21 at 14:54
  • 6
    You are correct, it seems that this feature is only available to .Net core projects. That's pity! Left a comment to avoid misguiding everyone in future. – Dipen Shah May 26 '21 at 15:12
  • I'm using VS 2022 Professional (not .Net core). The option is already selected in the settings, yet the menu option doesn't appear in Solution Explorer. Can anyone explain why? – priyamtheone Aug 19 '23 at 20:09
7

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

Used and works for Visual Studio 2010.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
eka808
  • 2,257
  • 3
  • 29
  • 41
  • 2
    You need to be careful with web projects. It reports DLLs that are used as "unused". – Miro J. Aug 29 '13 at 17:25
  • I agree with Miro. I tried to use this with my web project and it selected two dozen references that were required to run my web application (System.Web.*, Ninject.*, EntityFramework, just to name a few). – Denis M. Kitchen Oct 01 '13 at 17:11
  • Not working with MVC project. It remove essential DLL as .Helpers and .WebPage – User.Anonymous Sep 01 '14 at 15:59
2

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.

Aage
  • 5,932
  • 2
  • 32
  • 57
  • This tool will remove used references too, especially the reference related to EF – Jinu Feb 21 '22 at 10:12
  • I'm using VS 2022 Professional (not .Net core). The menu option doesn't appear in Solution Explorer. I checked `Tools > Options > Text Editor > C# > Advanced` wherein the option **Show "Remove Unused References" command in Solution Explorer (experimental)** is already checked. I unchecked and rechecked it. Yet it doesn't show the menu option in Solution Explorer. What could be the problem? Is there any solution/workaround for it? – priyamtheone Aug 19 '23 at 20:18
1

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
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
IndieTech Solutions
  • 2,527
  • 1
  • 21
  • 36
0

Very late to the party, but I happen to be working on a C# project right now. I was expecting Remove unused references to be available but sadly VS2022 C# Project type does not have such feature in the editor.

However, you could build whatever you are building, then load the DLL or executable up into Microsoft's ILSpy which allows you to clearly see which References it is using. Best of all, is free and ILSpy is a very useful tool in general.

enter image description here

codenamezero
  • 2,724
  • 29
  • 64
-3

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.

NoBrassRing
  • 483
  • 7
  • 15
  • The problematic part in this reply is "Step through each of your source files" which is not applicabile, in my project I have 900+ .VB files. EDIT: I LOLed reading the reply's author name, as the project is called BRASS. – Zac May 05 '21 at 09:30
-13

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.

sangram parmar
  • 8,462
  • 2
  • 23
  • 47
codelearner
  • 25
  • 11