The requested page is not available for the requested platform. You are viewing the content for Default platform.

Code Cleanup

  • 5 min to read

Code Cleanup allows you to apply Refactorings and Code Formatters to a document. During Code Cleanup, CodeRush analyzes code in the active file and applies certain Refactorings and Code Formatters where needed. This feature is helpful when you need to refactor a large amount of code.

Cleanup Refactorings

The following is a list of possible Cleanup Refactorings when performing Code Cleanup:


The hyperlinked Code Actions are available separately.

Title Description
Add copyright header Adds a copyright comment at the file's beginning (configuration)
Apply 'Attribute lists' style Combines several attributes into a single list or breaks attribute lists apart (according to the corresponding style rule)
Apply 'Attribute lists' style to assembly-level attributes Combines several assembly-level attributes into a single list or breaks attribute lists apart (according to the corresponding style rule)
Apply Braces in statements style Adds or removes optional block delimiters to/from all single-statement blocks (according to the corresponding style rule)
Apply built-in type style Converts explicitly-typed variable declarations to implicit or vice versa (according to corresponding style rule). The related Code Action is Make Implicit/Explicit
Apply 'Optional parenthesis' style Adds or removes optional parenthesis to/from the selected code elements (according to the corresponding style rule)
Apply variable declaration style Converts the CLR types to their keyword aliases or vice versa (according to corresponding style rule)
Apply visibility style Adds visibility modifiers where required or removes them where possible (according to corresponding style rule)
Apply 'this' qualifier style Adds 'this' qualifier where required or removes it where possible (according to corresponding style rule)
Collapse property accessors Converts all property accessors into a single-line form
Convert to String Interpolation Converts all formatted strings into the string interpolations
Convert to string.Empty Converts all empty string literals ("") into the string.Empty system constant
Expand property accessors Converts all property accessors into a multi-line form
Format document Executes the Document Formatting (C#) feature
Make properties auto-implemented Converts all properties with fully described accessors to auto-implemented properties
Organize members Sorts member declarations in the class and removes the redundant line breaks
Remove all comments, excluding XML comments Cleans the code from the non-XML comments
Remove all regions Cleans the code from the #region preprocessor directives
Remove redundant braces in blocks without parent statement Deletes optional block delimiters in "floating" code blocks
Remove redundant type cast Deletes the type casts in the places where they can be omitted
Remove redundant type qualifiers Deletes the type qualifiers in the places where they can be omitted and adds the required using/Imports statements
Remove redundant 'base' qualifier Deletes the 'base' qualifier in all member references where it can be omitted
Remove unused members Deletes the unused members
Remove unused namespace references Deletes the unused namespace references
Remove unused parameters Deletes the unused method parameters.
Remove unused types Deletes "internal", "private" and "private protected" nested types, and "internal" regular types
Remove unused private types Deletes unused private nested types
Remove unused variables Deletes the unused variables
Replace unused variables with discard Replaces unused variables with discard.
Sort modifiers Sorts member modifiers according to corresponding style rule
Sort namespace references Sorts namespace references alphabetically or by length
Use 'nameof' Converts all string literals that contain a variable or class member name to the nameof expression with the corresponding variable/member as an argument
Use expression bodies Converts all single-statement property and methods into expression-bodied structures


You can configure what Code Actions are executed during Code Cleanup. Open the Editor | <Language> | Code Cleanup options page and tick the required Code Actions to enable them. Click OK when you are finished.



The Cleanup Refactorings order in the options window defines their execution order.

Clean a File Up

Follow the steps below to execute Code Cleanup in an open file:

  1. Place the caret anywhere in your code.
  2. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
  3. Choose Code Cleanup from the menu.


Alternatively, you can use the following methods to execute Code Cleanup:

  • Visual Studio menu — open the CodeRush | Edit menu and choose Cleanup Document.
  • Visual Studio toolbar — use the Run Code Cleanup (CoceCleanup_icon) button on the CodeRush Visualize toolbar.

Clean a File Up on Save

CodeRush provides the "Apply Code Cleanup when saving a document" option, you can automatically clean up a file before it is saved.



CodeRush does not clean up a file when you close the unsaved file or solution.

Clean a Project Up

Follow the steps below to execute Code Cleanup in a project:

  1. Right-click the project in the Solution Explorer.
  2. Select Cleanup Project in the context menu.


When you cancel the process, the code remains unchanged.

Use Visual Studio's Undo action (Edit | Undo menu item) if you need to undo the cleanup after its completion. Note that this action is available when a document is open.


This feature is in a preview state because code cleanup can change your code's behavior or make it invalid. If this happens, undo the operation as described above.

Run Code Cleanup for Folder

You can run Code Cleanup for a selected folder (folders) in the Solution Explorer.

Right-click the folder (folders) in which you want to clean files and select CodeRush Cleanup.


A progress window appears when CodeRush cleans files for a selected folder (folders).


If code cleanup is cancelled while it is still in progress, all code remains unchanged.

See Also