IntelliJ IDEA 2021.2 Help

Safe delete

IntelliJ IDEA lets you use the Safe Delete refactoring to remove files and symbols from the source code safely. Before IntelliJ IDEA deletes a file or a symbol, it searches for usages and if they are found, IntelliJ IDEA lets you check them to make necessary adjustments.

Safe delete

  1. Select an item you want to delete.

  2. Select Refactor | Safe Delete from the main or context menu or press Alt+Delete.

  3. In the dialog that opens, select necessary options and click OK.

  4. If IntelliJ IDEA encountered potential problems, it displays the Usages Detected dialog. Proceed with the appropriate action.

Examples

Use case

Before

After

Safe delete a parameter for a call hierarchy (here performed on the parameter i within baz(int i)).

If a parameter is passed only through a call hierarchy, the Safe Delete action will delete the parameter through the whole hierarchy. IntelliJ IDEA displays the appropriate dialog where you can select the caller methods in which the parameter should be deleted.

class CallHierarchySample { private void foo(int i) { bar(i);} private void bar(int i) { baz(i);} private void baz(int i) { } }
class CallHierarchySample { private void foo() { bar();} private void bar() { baz();} private void baz() { } }

Safe delete a method in a call hierarchy (here performed on the foo(int i) method).

IntelliJ IDEA analyzes the corresponding call hierarchy and displays the appropriate dialog, suggesting you delete all unused methods in that call hierarchy.

class CallHierarchySample { private void foo(int i) { bar(i);} private void bar(int i) { baz(i);} private void baz(int i) { } }
class CallHierarchySample { }

Safe delete the unused class field (here performed on myProperty).

When you remove an unused class field that is injected via constructor, IntelliJ IDEA removes the associated constructor parameter as well. IntelliJ IDEA opens the appropriate dialog where you can check and confirm your code deletion.

public class MyClass{ private final MyProperty myProperty; public MyClass(MyProperty myProperty){ this.myProperty = myProperty; } }
public class MyClass { public MyClass(){ } }
Last modified: 02 August 2022