Show TOC

Deleting Unused VariablesLocate this document in the navigation structure

Context

In a source code-based development object, you can delete unused variables in the entire object or only in marked source code. This enables you to automatically find and delete unused variable declarations in order to clean up source code.

This function deletes the following types of unused variables:

  • Local variables within the scope of a method, function module, or form routine
  • Global variables within a class, function group, or report.
    Note In a class, no variables that are declared in the PUBLIC or PROTECTED sections are deleted. The deletion does not delete seemingly unused variables that could be referenced from outside.
Note Deleting an unused variable can cause another variable to become unused if it was referenced. The deletion of unused variables does not perform recursive checks for such variables. It can therefore be useful to run the function more than once in order to find and delete unused variables in such cases.

You can undo the deletions using the shortcut Ctrl Z.

Example

CLASS cl_car_unused_variable DEFINITION PUBLIC.
 PUBLIC SECTION.
  METHODS drive IMPORTING i_speed TYPE i.
ENDCLASS.
 
CLASS cl_car_unused_variable IMPLEMENTATION.
 METHOD drive.
  DATA: speed TYPE i,
      max_speed TYPE i.
  speed = 50.
 ENDMETHOD.
ENDCLASS.

Procedure

  1. You can delete all unused variables of a source code-based development object:
    • In the source code editor, position the cursor anywhere within the source code.
    • In the context menu, choose Start of the navigation path Source Next navigation step Delete Unused Variables (All) End of the navigation path or shortcut (Alt U).
  2. You can delete all unused variables in a selected section of source code:
    • In the source code editor, select the source code in the method where you want to perform the deletion.
    • In the context menu, choose Start of the navigation path Source Next navigation step Delete Unused Variables (Selection) End of the navigation path or shortcut (Alt Shift U).

Results

The unused variable declarations of the selected section are removed and its lines are highlighted with a marker in the ruler. The marker disappears after you have saved or checked the syntax.

Example

ABAP class after deleting:

CLASS cl_car_unused_variable DEFINITION PUBLIC.
 PUBLIC SECTION.
  METHODS drive IMPORTING i_speed TYPE i.
ENDCLASS. 
 
CLASS cl_car_unused_variable IMPLEMENTATION.
 METHOD drive.
  DATA: speed TYPE i.
  speed = 50. 
 ENDMETHOD.
ENDCLASS.
Popup that highlights the deletion of an unused variable definition with a minus
Figure 1: Popup that highlights the deletion of an unused variable definition with a minus
Note Hover the cursor under the marker to display the deleted statement(s).