89

I am using visual studio 2010 and was wondering if there is a way to automatically remove all the namespaces that aren't being used at the the top of the page e.g.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

in the above example System.Collections.Generic is not being used is there a way to know that without trial and error.

user1496355
  • 893
  • 1
  • 6
  • 5
  • 2
    Resharper 7 has a new 'remove all unused Usings' for entire solution button ;-) – IvoTops Aug 22 '12 at 15:00
  • 2
    I just wonder how i remove unused dll's which added as reference before – Mustafa Ekici Aug 22 '12 at 15:02
  • @IvoTops: For stingy people like myself that haven't (yet) spent my money on ReSharper, its actually possible to do in VS2010 directly using macros. See the blog post in my answer below if you are interested (or stick to resharper that obviously does this without any hustle :) – Avada Kedavra Aug 22 '12 at 19:09
  • @Avada Kedavra:I made it a comment because recommending products is not my favourite way of answering. But in this case I couldn't resist. And I do love ReSharper. Well worth the money, imho. – IvoTops Aug 23 '12 at 08:06
  • For Visual Studio 2019 and later, this question has been re-asked. See [https://stackoverflow.com/questions/55615136](https://stackoverflow.com/questions/55615136/) – Jordan Ryder May 12 '22 at 13:12

9 Answers9

133

Yes you can right click on the page and from the menu select;

Organise Usings > Remove Unused Usings

Alternatively if you prefer shortcuts, please use;

Ctrl + R + G

I am using this all the time, to clean up code, and make code compilation quicker.

Or you can use PowerCommands to remove unused usings for entire projects

saj
  • 4,626
  • 2
  • 26
  • 25
  • 2
    I personally use remove and sort to keep things neat – Jesus Ramos Aug 22 '12 at 14:58
  • The only text I see is `Organize Usings > Sort Usings`. Does this mean that all the usings are used? – Al Lelopath Jun 23 '16 at 20:37
  • Here is an updated extension: [Power Commands for Visual Studio](https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.PowerCommandsforVisualStudio), works with VS 2017 – Homer Jun 08 '18 at 15:58
  • How can I run this command on all the XAML files in a project ? Visual Studio and Resharper do remove unused references but not in XAML files which is a shame. – Devid Apr 14 '20 at 10:51
  • How can you do this for the entire solution? – Mugurel Dec 03 '21 at 08:10
  • I changed it to CTRL-SHIFT-O cuz that makes way more sense - Organize imprts. That's the same shortcut as in Eclipse. It's a shortcut we should be using a lot so R G I guess is short for Remove and Group but that's way harder to remember esp switching between Java and C# every year or two. – Skystrider Dec 13 '21 at 21:41
59

In Visual Studio 2017, I use this very simple key combinations: CTRL+R+G to remove unused imports/namespaces

Update:

In VS for Mac:

Unfortunately, there's no default key binding for this but you can always edit your preferences.

Visual Studio -> Preferences

enter image description here In my case, it is binded as +R+G

mr5
  • 3,438
  • 3
  • 40
  • 57
39

You can install an extension called: Productivity Power Tools 2015 (from Microsoft) and it has an option to automatically remove unused usings when you save your file. No more work needed after.

enter image description here

enter image description here

Henk-Martijn
  • 2,024
  • 21
  • 25
  • 6
    Worth noting, in VS 2017 this is broken out into the Power Commands extension. – bbodenmiller Mar 08 '17 at 08:44
  • 4
    in VS 2017 formatting and removing unused using statements **is not working** – tchelidze Apr 01 '17 at 08:16
  • Not sure if you are using ReSharper, but it can cause Power Commands to break in VS 2017. Try unchecking 'Hide overridden VS menu items' in [ReSharper Options](https://resharper-support.jetbrains.com/hc/en-us/community/posts/115000159930-VS-Productivity-Power-Tools-Remove-and-Sort-Usings-on-Save-not-working-with-R-?page=1#community_comment_115000202364). – Drew Aug 07 '17 at 00:45
  • Great answer, thanks for the tip! You could improve your answer by showing how you can install extensions in Visual Studio. I found out how to do it now, but would be easier if you include this into your answer. – Dennis Schröer Aug 23 '17 at 05:56
38

If you are using Visual Studio 2019, you can:

Right click your solution -> Analyze and Code Cleanup -> Configure Code Cleanup -> Create a profile (Remove & Sort usings) and then run it.

enter image description here enter image description here

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
12

For VS 2019, VS 2022 or ReSharper. If you add an unused using to the top of a file such as using System.IO; a light bulb appears. Clicking on the light bulb gives the option to remove the unused usings from either the file, folder, project or entire solution.

enter image description here

enter image description here

user1069816
  • 2,763
  • 2
  • 26
  • 43
6

Same answer as everyone else. If you are a keyboard ninja (Shift+F10 pops up contextual menu), use this:

Shift+F10,O,R


What do you call that special key for contextual menu? Instead of pressing Shift+F10 (my keyboard don't have that special key, that's why I uses Shift+F10), that is shorter:

whateverThatKeyCalled, O, R

Michael Buen
  • 38,643
  • 9
  • 94
  • 118
6

Update:

For VS 2019 and later, there are better answers, see For Visual Studio 2019 and later, this question has been re-asked. See In Visual Studio 2019 how can I remove unused usings on format document?

Original Answer:

I know the OP specified VS 2010, but that was a while ago and this feature is built right into VS 2017. From the Menu, Tools -> Options, then find the check box below.

After that, any time you use CTRL + K , CTRL + D they will be removed.

Remove unnecessary usings

Jordan Ryder
  • 2,336
  • 1
  • 24
  • 29
4

I am using Visual Studio 2019 and for some reason my project settings have a Warning level of 3 and this prevent Visual Studio from cleaning the usings. Make sure this value is set to 4.

Settings -> Build -> Warning Level = 4

Jaider
  • 14,268
  • 5
  • 75
  • 82
3

As saj is saying you can use the Remove Unused Usings, which works great and even greater if you sort the usings at the same time. Resharper 4.5 can help you do this for entire projects which is a functionality that is missing (and is missed) in VS2010.

More info provided in the following thread: Remove unused Usings across entire assembly


Tip: The thread above refers to the blog that proposes a macro to remove unused usings across the entire solution. Organize Usings Across Your Entire Solution. I just tried it out, and it worked like a charm!

bta
  • 43,959
  • 6
  • 69
  • 99
Avada Kedavra
  • 8,523
  • 5
  • 32
  • 48