Technical Articles
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:
- Open the source code editor of the development object.
- 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:
- Open the source code editor of the development object.
- Select the code where you want to delete the unused variables.
- 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:
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.
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.
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
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.
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.