Safely removes dead code and consolidates duplicates in Salesforce Apex and LWC using PMD and Salesforce Code Analyzer for cleanup.
How this skill is triggered — by the user, by Claude, or both
Slash command
/salesforce-claude-code:refactor-cleanThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Remove dead code and consolidate duplicates safely. Uses Salesforce Code Analyzer (`sf code-analyzer run`) for detection when available, falls back to manual analysis.
Remove dead code and consolidate duplicates safely. Uses Salesforce Code Analyzer (sf code-analyzer run) for detection when available, falls back to manual analysis.
Option A: Salesforce Code Analyzer (preferred)
sf code-analyzer run --target force-app --format table
sf code-analyzer run --target force-app --format json | jq '.[] | select(.severity <= 2)'
Option B: Manual analysis
# Find Apex classes with no references.
# Uses sfdx-project.json packageDirectories to find all source paths,
# rather than hardcoding force-app/main/default.
# Note: This heuristic searches source files for class name strings. Results may
# be incomplete — classes can be referenced dynamically (Type.forName), from
# managed packages, external integrations, or metadata not in source control.
# Always verify before deleting.
SRC_DIRS=$(node -e "
const p = require('./sfdx-project.json');
console.log(p.packageDirectories.map(d => d.path).join(' '));
" 2>/dev/null || echo "force-app")
for dir in $SRC_DIRS; do
find "$dir" -name "*.cls" -not -name "*Test*" | while read cls; do
name=$(basename "$cls" .cls)
# Note: Use word-boundary matching for precise results to avoid partial name matches
refs=$(grep -rlw "$name" $SRC_DIRS --include="*.cls" --include="*.trigger" --include="*.js" --include="*.xml" 2>/dev/null | grep -v "$cls" | wc -l)
if [ "$refs" -eq 0 ]; then echo "UNREFERENCED: $name"; fi
done
done
# Find unused methods within a class (private methods not called internally)
grep -n "private.*void\|private.*String\|private.*List\|private.*Map" <file>.cls
Categorize each finding before removing anything:
| Safety Level | Criteria | Action |
|---|---|---|
| SAFE | Private methods with no internal callers, unreferenced private classes, commented-out code blocks | Remove immediately |
| CAUTION | Public methods not referenced in code (may be called by Flows, Process Builders, or external systems) | Check metadata references first |
| DANGER | global methods, @AuraEnabled, @InvocableMethod, @InvocableVariable, @RemoteAction, @HttpGet/Post, managed package components | Do NOT remove without explicit verification |
Before removing any CAUTION or DANGER items:
# Check if a method is referenced in Flows
grep -rl "ClassName.MethodName" force-app/main/default/flows/
# Check if a class is used in Process Builders
grep -rl "ClassName" force-app/main/default/workflows/
# Check if a class is referenced in Custom Metadata
grep -rl "ClassName" force-app/main/default/customMetadata/
# Check if an LWC component is used in FlexiPages
grep -rl "c-component-name" force-app/main/default/flexipages/
# Check if a class is referenced in page layouts
grep -rl "ClassName" force-app/main/default/layouts/
Never remove without checking:
@AuraEnabled methods — may be called by LWC/Aura components not in your source@InvocableMethod — may be called by Flows you can't see in source controlglobal methods — may be called by subscriber orgs or external packageswebService methods — may be called by external integrationsFor each SAFE item:
sf apex run test --test-level RunLocalTests --wait 15sf project deploy start --dry-run --wait 10After dead code removal, look for consolidation opportunities:
/checkpoint refactor-start@AuraEnabled, @InvocableMethod, or global without verifying all callersrefactor-clean Remove unused Apex classes and dead methods from force-app/
refactor-clean Consolidate duplicate utility methods across AccountService and ContactService
refactor-clean Find and remove unreferenced LWC components
refactor-clean Run Salesforce Code Analyzer and clean up all HIGH severity PMD findings
npx claudepluginhub jiten-singh-shahi/salesforce-claude-code --plugin salesforce-claude-codeRuns Salesforce Code Analyzer to scan Apex, LWC, and JS code for security, performance, style, and duplicate violations. Supports all engines (PMD, ESLint, CPD, RetireJS, Flow, SFGE, ApexGuru) and targets including git diff.
Reviews Salesforce Apex classes, triggers, LWC, and async jobs for security vulnerabilities, governor-limit risks, and sharing enforcement issues. Works from pasted code only.
Analyzes and removes dead code in Repowise-indexed codebases using graph analysis. Suggests safe deletion order and flags false positives.