IntelliJ IDEA 2016.2 Help

Safe Delete

On this page:

Introduction

The Safe Delete refactoring lets you safely remove files and symbols from the source code.

To make sure that deletion is safe, IntelliJ IDEA looks for the usages of the or symbol being deleted. If such usages are found, you can explore them and make the necessary corrections in your code before the symbol is actually deleted.

Performing the refactoring

  1. Select the item to be deleted.
  2. Do one of the following:
    • Press Alt+Delete.
    • Select Refactor | Safe Delete from the main or the context menu.
    • Select Refactor | Refactor This from the main menu (Ctrl+Shift+Alt+T), and select Safe Delete.
  3. In the Safe Delete dialog, select the necessary options and click OK.
  4. If the refactoring is potentially unsafe, the Usages Detected dialog opens.
    • View Usages. Click this button to see where in your code the item you are about to delete is used. As a result, the Find tool window opens.

      Analyze your code and make the necessary corrections. Then click Do Refactor. (If you want to rerun the refactoring from its start, click Rerun Safe Delete. IntelliJ IDEA will check if the refactoring is safe once more.)

    • Delete Anyway. Click this button to delete the item without looking at its usages.

Safe Delete Parameter refactoring for a call hierarchy

If a parameter is only passed through a call hierarchy and isn't used anywhere outside of that hierarchy, the Safe Delete refactoring lets you propagate the parameter deletion all along the hierarchy.

Example

BeforeAfter
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() { } }

In this example, the refactoring was performed on the i parameter within baz(int i). This change was propagated to all the caller methods.

When performing the refactoring, the Select Methods to Propagate Parameter Deletion dialog is shown. This dialog lets you select the caller methods in which the parameter should be deleted.

SelectMethodsToPropagateParameterDeletion

Safe Delete refactoring for a method in a call hierarchy

When you perform the Safe Delete refactoring for a method, IntelliJ IDEA analyzes the corresponding call hierarchy, finds the methods that may become unused and suggests that you delete those methods too.

Example

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

In this example, the refactoring was performed on the foo(int i) method. All the methods lower down in the call hierarchy were selected for deletion.

When performing the refactoring, the Select Methods to Cascade Safe Delete dialog is shown. This dialog lets you select the methods to be deleted.

SelectMethodsToCascadeSafeDelete

See Also

Last modified: 23 November 2016