agentleFS
jiten-singh-shahijiten-singh-shahi/salesforce-claude-code/.cursor/skills/refactor-clean/SKILL.md

refactor-clean

Skill17 starsChanged 8 months agoMITView on GitHub

Use when cleaning up Salesforce Apex or LWC code. Dead code removal and consolidation using PMD and Salesforce Code Analyzer for safe cleanup.

10 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use audit-choices, close-spec and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • dzhngSkill

    audit-choices

    Audit the choices an implementing agent made, not its diff — a pure decision audit that traces the session's history into a choices ledger, changes no code, and never blocks an unsupervised run. Working code still embeds architecture the user never chose; surface it because future work inherits it. Use when the user wants to review the decisions the AI made on their behalf, before merging or committing AI-implemented work, when integrating a delegated subagent's pass, or when a fix "works" but might be a point fix.

    Also in 3 projects that use this

  • dzhngSkill

    close-spec

    When a spec under specs/<feature>/ is done shipping (a write-spec build, or any planned task that produced a spec), archive it to specs/done/ and rewrite it from a build-plan into a durable rationale — the why, the principles, the invariants — pointing back to the real code for the how. Use when implementation has landed and the plan no longer matches what shipped, or the user says a feature/spec is finished. Pairs with [write-spec](../write-spec/SKILL.md) (the plan this closes) and [review](../review/SKILL.md) (run before closing).

    Also in 3 projects that use this

  • LucasMatuszewskiSkill

    audit-agents

    Audit and maintain agent configuration files (.claude/agents, AGENTS.md, CLAUDE.md, agent-memory). Checks for duplicates, outdated info, naming conflicts, wrong technical facts, and files over 200 lines. Fixes all issues and commits.

    Also in 3 projects that use this

  • Halcyon666Skill

    write-skills

    Creates high-quality Agent Skills (SKILL.md files) that follow the open Agent Skills specification. Use when asked to create a skill, write a SKILL.md, package agent instructions, build a reusable capability, or turn a workflow into a shareable skill. Produces spec-compliant, concise, well-structured skills optimized for agent discovery and execution.

    Also in 3 projects that use this

  • isLinXuSkill

    eval-skills

    AI Agent Skill unit testing framework. A framework-agnostic toolkit for discovering, scaffolding, selecting, evaluating, and reporting on AI skills. Use this skill to assess skill quality before production, compare candidate skills on the same benchmark, enforce quality gates in CI/CD, and generate human-readable evaluation reports.

    Also in 3 projects that use this

  • TrecekSkill

    audit-tests

    Audit the test suite for useless tests, consolidation opportunities, over-mocking, weak assertions, and other test quality issues. Use when user says "audit tests", "audit test suite", "review tests", or "test quality check". Generates an improvement plan in temp/ with explanations for each proposed change.

    Also in 3 projects that use this

  • +6 more from dzhng

The file

Show its rules and text

We split this skill into the rules it gives an agent

19 rules. 0 are common, 16 are distinctive, and 3 are very specific to this project.

  • 0

    Common

    Most of the 10 edited versions we read keep these.

  • 16

    Distinctive

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Remove dead code and consolidate duplicates safely.

    line 10
  2. Distinctive:

    When cleaning up unused Apex classes, methods, or LWC components

    line 14
  3. Distinctive:

    When consolidating duplicate utility methods across multiple service classes

    line 15
  4. Distinctive:

    When preparing a codebase for a security review or AppExchange submission

    line 16
  5. Distinctive:

    When Salesforce Code Analyzer or PMD reports high-severity findings that need cleanup

    line 17
  6. Distinctive:

    When reducing technical debt by removing commented-out code and unreferenced classes

    line 18
  7. Distinctive:

    Before removing any CAUTION or DANGER items:

    line 70
  8. Distinctive:

    Never remove without checking:

    line 91
  9. Distinctive:

    Delete the dead code

    line 103
  10. Distinctive:

    If tests fail, revert the deletion immediately

    line 106
  11. Distinctive:

    After dead code removal, look for consolidation opportunities:

    line 110
  12. Distinctive:

    Update all callers to use the shared implementation

    line 114
  13. Distinctive:

    Run full test suite after each consolidation

    line 115
  14. Very specific:

    Always create a checkpoint before starting: /checkpoint refactor-start

    line 119
  15. Distinctive:

    Remove one item at a time — never batch deletions

    line 120
  16. Distinctive:

    Run tests after each removal

    line 121
  17. Very specific:

    Never remove @AuraEnabled, @InvocableMethod, or global without verifying all callers

    line 122
  18. Distinctive:

    Check metadata references (Flows, Process Builders, FlexiPages) before deleting any public method

    line 123
  19. Distinctive:

    Keep a log of what was removed and why

    line 124

“Common” is measured: kept by at least 6 in 10 of the 10 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Refactor Clean — Dead Code Removal and Consolidation

Remove dead code and consolidate duplicates safely. Uses Salesforce Code Analyzer (sf code-analyzer run) for detection when available, falls back to manual analysis.

When to Use

  • When cleaning up unused Apex classes, methods, or LWC components
  • When consolidating duplicate utility methods across multiple service classes
  • When preparing a codebase for a security review or AppExchange submission
  • When Salesforce Code Analyzer or PMD reports high-severity findings that need cleanup
  • When reducing technical debt by removing commented-out code and unreferenced classes

Workflow

Step 1 — Detect Dead Code

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

Step 2 — Classify Safety

Categorize each finding before removing anything:

Safety LevelCriteriaAction
SAFEPrivate methods with no internal callers, unreferenced private classes, commented-out code blocksRemove immediately
CAUTIONPublic methods not referenced in code (may be called by Flows, Process Builders, or external systems)Check metadata references first
DANGERglobal methods, @AuraEnabled, @InvocableMethod, @InvocableVariable, @RemoteAction, @HttpGet/Post, managed package componentsDo NOT remove without explicit verification

Step 3 — Check Metadata References

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/

Step 4 — Managed Package Considerations

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 control
  • global methods — may be called by subscriber orgs or external packages
  • webService methods — may be called by external integrations
  • Methods referenced in Permission Sets or Custom Permissions

Step 5 — Remove One at a Time

For each SAFE item:

  1. Delete the dead code
  2. Run tests: sf apex run test --test-level RunLocalTests --wait 15
  3. Verify deployment: sf project deploy start --dry-run --wait 10
  4. If tests fail, revert the deletion immediately

Step 6 — Consolidate Duplicates

After dead code removal, look for consolidation opportunities:

  1. Identify similar methods across classes (same logic, different names)
  2. Extract shared logic into a utility/service class
  3. Update all callers to use the shared implementation
  4. Run full test suite after each consolidation

Safety Rules

  • Always create a checkpoint before starting: /checkpoint refactor-start
  • Remove one item at a time — never batch deletions
  • Run tests after each removal
  • Never remove @AuraEnabled, @InvocableMethod, or global without verifying all callers
  • Check metadata references (Flows, Process Builders, FlexiPages) before deleting any public method
  • Keep a log of what was removed and why

Examples

refactor-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

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.