Skip to main content

Code reviews rule: Unused Apex method

Written by David Martin
Updated today

Unused Apex method

Why is this an issue?

Code that is never used adds noise to code reviews, complicates refactoring, and makes the codebase harder to navigate. Developers are often reluctant to remove code they did not write, often causing dead code to compound over time.

Dead code is best removed as soon as it is identified, ideally as a standalone change to keep the diff focused.

This rule constitutes several different dead code detections, and will raise violations for:

  • Unused methods - methods with no callers anywhere in the codebase

  • Empty methods - methods with no statements in their body

  • Empty private methods - private (or implicitly private) methods with no body

  • Unused interfaces - interfaces that no class implements

  • Empty Aura methods - empty function bodies in Aura controller or helper files

Examples

Examples of incorrect code:

public class AccountService {
// Unused method - never called from anywhere
private void legacyUpdate(Account acc) {
acc.Legacy_Field__c = true;
update acc;
}

// Empty method - no statements in body
public void onBeforeInsert(List<Opportunity> records) {
}

// Implicitly private empty method - no access modifier defaults to private
void unusedSetup() {
}
}
// Unused interface - no class implements it
public interface INotificationService {
void sendAlert(String message);
}
// Empty Aura controller method
({
handleClick : function(component, event, helper) {
}
})

In all cases, the fix is to remove the dead code.

How can I fix violations?

To resolve a violation, follow these steps:

  1. Verify the method is truly unused: Search for references across Apex classes, triggers, Visualforce pages, Lightning components, and flows.

  2. Check for dynamic invocation: Methods called via reflection or Type.forName() may not show direct references.

  3. Delete the method: Once confirmed unused, remove it from the codebase. For empty private methods, autofix is available and can remove the method automatically.

  4. Remove related test code: If test methods exist solely for the deleted method, remove them too.

The unused method detection does not flag methods that are likely called externally:

  • global methods

  • override methods

  • webservice methods

  • Methods annotated with @AuraEnabled, @InvocableMethod, @RemoteAction, @NamespaceAccessible, @HttpGet, @HttpPost, @HttpPut, @HttpPatch, or @HttpDelete

  • Methods that implement an interface

Test classes are skipped entirely, since test methods are invoked by the test runner rather than called from other code.

When should I disable this rule?

You may dismiss specific violations for:

  • Public API methods in managed packages that must be preserved for backwards compatibility

  • Methods called dynamically via reflection or Type.forName()

  • Apex controller methods referenced from Visualforce markup (e.g. {!doSomething} action bindings) that are not annotated with @RemoteAction

Resources

Did this answer your question?