Clean Up & Sort Unused .NET Using Statements in Visual Studio

I needed to do a refactor that broke a namespace into multiple namespaces. Their may be some other way to do this in Visual Studio. The easiest way I thought of was finding & replacing the old namespace with the new namespaces. This meant I would be adding namespaces to files they weren't used in. Not a big deal, but something I prefer to avoid.

Fortunately for me, Visual Studio added a sweet new clean up feature in version 15.8. It even works on the free Community version.

Format Document Settings Window

Go to Tools > Options > Text Editor > C# > Code Style > Formatting.

It has an experimental Format Document Settings where you can have Visual Studio clean up your code. Two of the options are to "Remove unnecessary usings" and "Sort usings".

Check these two and you're good to go! No need for an extension or ReSharper to do this anymore.


Update: For Visual Studio 2019, it appears this will be living in a different location named "Configure Code Cleanup". You can open up the configuration these ways:

  1. The easiest way to find anything in Visual Studio is click Ctrl+Q or click the "Search Visual Studio" toolbar & type it in.

  2. In the menu go to Analyze > Code Cleanup > Configure Code Cleanup.

Once you've configured code cleanup, you need to run it. Here are your options:

  1. The first profile you've setup you can run with Ctrl+K, Ctrl+E.

  2. On the bottom (leftish) of your screen there is a tiny broom icon. Click that.

  3. In the menu go to Analyze > Code Cleanup. Then select the cleanup profile you want to run.

  4. You can run code cleanup on save with Mads Kristensen's extension.

One Last Thing...

If you have a question or see a mistake, please comment below.

If you found this post helpful, please share it with others. It's the best thanks I can ask for & it gives me momentum to keep writing!

Matt Ferderer
Software Developer focused on making great user experiences. I enjoy learning, sharing & helping others make amazing things.
Let's Connect