We normally come into situation that, before production deployment we need to remove system.debug('*****'); statements from the code.

I am wondering if there any existing tool is available for such requirement where instead of doing manual work I can remove these statements automatically! or if I have to build this functionality will Tooling API would be the best approach to get started with? as I would need to parse the apex, identify the instances in code where system.debug exist and then do the necessary operation to replace it.


Using sublime text you can use the feature

Find > find in files (CTRL+Shift+F)

Activate regular expression by clicking the button .*

Search for System.debug(.*) and replace it with an empty string


Customize the where clause to target the file extensions you want


Removing debugs logs would be pain, if you perform search and replace in your editor, it would not be efficient.

Better and easier option would be to create ​a utility method for logging your debug statements. Control it with custom settings.

By that you can enable it back when something goes wrong.


Personally, I think a good tool for this kind of text manipulation is sed. If you navigate to your classes directory, this command should replace all system.debug() statements with a dummy variable assignment.

$ sed -i 's/system\.debug(.*);/Integer ThisVariableNameIsProbablyNotAlreadyBeingUsed = 3;/g' *

I would suggest you not simply rip out those statement, and replace them with a dummy expression instead because you could change the behavior of you code if, for example, you have a for loop followed directly by a debug statement rather than a code block.

for (...) system.debug(...);
... something that shouldn't be looped ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.