Anthony
Anthony

Reputation: 1837

In Visual Studio 2019 how can I remove unused usings on format document?

In Visual Studio 2019 how can I remove unused usings on format document?

I have found instructions for previous versions of Visual Studio (Go to Tools > Options > Text Editor > C# > Code Style > Formatting.). I don't see that in Visual Studio 2019.

Upvotes: 62

Views: 52575

Answers (9)

anil shrestha
anil shrestha

Reputation: 3240

For single page :

Ctrl+K+E

For all projects:

  1. Right click to your solution on your Solution.

  2. Click on Analyze and Code Cleanup followed by Run Code Cleanup (Profile 1) enter image description here

  3. And all done, it cleans and formats all over the project.

  4. If you need some more setting on code cleanup or if it don't work you need to click on configure code cleanup below cleanup profile.

  5. And then add necessary settings. enter image description here

Upvotes: 11

Jan
Jan

Reputation: 1779

Ctrl + K + E will sort and remove your unused usings.

Upvotes: 52

Dush
Dush

Reputation: 1365

We can remove it by clicking on the light bulb and selecting" Remove Unnecessary Usings". In addition to removing it from the current document, if we closely look at the bottom of the popup, it can be done for the Project or Solution as well. No need to install or configure anything. This feature is available on VS2015, VS2017, VS2019 and VS2022 on my machine.

enter image description here

Upvotes: 8

Lemraj
Lemraj

Reputation: 389

I always use the combination CTRL + R, CTRL + G. This sorts the usings and removes the unused ones.

Upvotes: 6

Garrett
Garrett

Reputation: 91

The keyboard command that works for me is Edit.RemoveAndSort

Upvotes: 8

M.Salehi
M.Salehi

Reputation: 161

First : go Extensions - > install "Productivity Power tools" then "Power Commands for Visual Studio" extentsion

Second : go tools menu -> Options -> Productivity Power tools -> PowerCommands -> General -> checked "Remove and Sort Usings on save"

note : These options are available only when you use "Power Commands for Visual Studio".

Upvotes: 16

Reven
Reven

Reputation: 776

There is no need to call it by hand, you can use 'cleanup on save' see this post

Upvotes: 0

wale A
wale A

Reputation: 81

You can assign both functions to Ctrl + K, Ctrl + D by changing hotkeys and keyboard shortcuts

Upvotes: 0

Sergey Vlasov
Sergey Vlasov

Reputation: 27920

In Visual Studio 2019 you can remove unused usings with the Code Cleanup command:

enter image description here

enter image description here

And running code cleanup also formats the document.

Upvotes: 68

Related Questions