Skip to Content
Technical Articles
Author's profile photo Wolfgang Woehrle

Deleting Unused Variables with ABAP in Eclipse

Can you find variables that are no more used in a certain ABAP program or ABAP class? You can delete these variables in a selected part of the source code or in the whole development object in order to make your source code more understandable.

This function considers:

  • 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.

Procedure

You can delete all unused variables of the whole development object:

  1. Open the source code editor of the development object.
  2. Within the source code, choose Source > Delete Unused Variables (All) (Ctrl + U) from the context menu.

You can delete all unused variables of a selected area:

  1. Open the source code editor of the development object.
  2. Select the code where you want to delete the unused variables.
  3. From the context menu, choose Source > Delete Unused Variables (Selection) (Ctrl + Shift U).

Result

The unused variable declarations are removed from your source code. All the lines of the former occurrences where an unused variable has been deleted are marked:

/wp-content/uploads/2013/07/delete_unused_247237.jpg

If you hold the cursor above this mark, you will see what has been deleted in a popup. In case if you are unsatisfied, choose Ctrl + Z to undo all of the changes.

NOTE

Check your code using Check ABAP Development Object (Ctrl + F2).

NOTE

Deleting an unused variable can cause another variable to become unused. 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.

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hallo Wolfgang, thanks for the Doc.

      But I can not find the Function in Method Code Editor, there is no 'Source' in the Toolbar.

      Could you tell me, what should I do?

      Thanks in advance.

      Author's profile photo Michael Schneider
      Michael Schneider

      Hi Yinxiao,

      Wolfgang described this functionality for ABAP development tools in Eclipse (not for SE24 in SAP GUI). Please let me know, if you are using Eclipse. If so, this seems to be a bug.

      Michael

      Author's profile photo Yellappa Madigonde
      Yellappa Madigonde

      Hi Wolfgang,

      I tried your approach with global class methods but it seems it is not working for class methods. My class has few unused variables and expecting these would be cleared. SAP clearly states that it removed local variables in a method. What could be the reason?

       

      Regards,

      Yellappa.

      Author's profile photo Richard Harper
      Richard Harper

      I think that the fact it is eclipse needs to be added to the title - I spent some time trying to make this work in SE80.