Introduction

Visual Studio 2022 provides a Code Cleanup feature that formats your code and applies code style rules automatically. This helps keep your codebase clean, readable, and maintainable.

What is Code Cleanup?

Code Cleanup applies refactoring and formatting rules to your code files. It includes:

How to Use Code Cleanup

1. Using the Toolbar Button

2. Using a Keyboard Shortcut

3. Context Menu

code clean up

Configure Code Cleanup Profiles

  1. Go to: Tools > Options > Text Editor > [Your Language, e.g., C#] > Code Style > Code Cleanup
  2. There are two profiles (Profile 1 & Profile 2). Each can be customized with:
    • Format document

    • Sort and remove usings

    • Apply file header

    • Apply naming rules

    • Remove unnecessary code

  3. Click Configure Code Cleanup to choose what each profile should do.

    Configure Code Cleanup

Run Cleanup on Entire Solution or Project

  1. Right-click on the Solution or Project in Solution Explorer.

  2. Choose Analyze and Code Cleanup > Run Code Cleanup.

Note: By default, this only formats open documents. To apply it to all files, use a tool like Roslyn analyzers or the dotnet format CLI (for .NET Core/.NET 5+ projects).

Optional Tools for Deeper Cleanup

Conclusion

In this article, I have tried to cover Code cleanup in Visual Studio 2022.