Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically remove unused code with Resharper

I clicked "Find Code Issues", then Resharper showed me list of unused classes and methods. But I can't find how to automatically remove all them. List of unused classes and methods contains thousands of lines, so сlicking on each line and delete method manually is not real. How to do it automatically?

I tried to use "Code Cleanup", but it doesn't remove unused classes and methods

The version of Resharper: JetBrains ReSharper 8.2.1 Full Edition Build 8.2.1000.4556 on 2014-05-19T09:12:38

like image 608
Anatolii Humennyi Avatar asked May 30 '14 12:05

Anatolii Humennyi


People also ask

How do I run ReSharper cleanup?

Select ReSharper | Options from the main menu or press Alt+R O . Go to the cleanup profiles settings page: Code Editing | Code Cleanup | Profiles. Select which Code Cleanup profile should be applied on save and click Set as default (the default profile is also used for silent cleanup).

How do I remove unused NuGet packages?

Clean up project references and NuGet packages in Visual Studio. Firstly, Right Click on the Project, and select “Remove Unused References”. This will bring up the “Remove Unused References” Dialog that shows the projects and all unused packages. Here, you can again choose if you want to remove them or keep it as it is ...

How do I remove a redundancy code?

Go to the cleanup profiles settings page: Code Editing | Code Cleanup | Profiles. Create a new profile as described in the Create a new custom cleanup profile section. In the Selected profile settings section for the new profile, tick the Remove code redundancies checkbox.


1 Answers

Unfortunately Resharper does not provide this feature as it can be unsafe.

To partially automate the deletion you can try installing AutoHotkey. This program will allow you to automate the entry of hotkeys and therefore 'automate' repetitive Resharper tasks like deleting unused code detected by Code Issues.

The following script automatically goes to the next code issue and attempts to 'Safely Delete'. If this takes too long it cancels it with {Escape} as this is generally means Resharper has found a conflict or usage.

#d::
    Loop {
        Send !{Del}
        Sleep, 500
        Send {Enter}
        Sleep, 1000
        Send {Escape}
        Sleep, 500
        Send {F8}
        Sleep, 500
    }
Return

To use this script click on the first item under the Type Or Member is Never Used category then hit the Win-D hot key. The script will then cycle through all the issues deleting the methods which do not have conflicts. To break the loop select outside of Visual Studio & Reload the script.

If you remove the Loop & {Escape} then you can use this as a single shortcut to delete & move to the next issue.

like image 175
Daniel Roberts Avatar answered Sep 18 '22 21:09

Daniel Roberts