Remove unused imports, functions, variables, commented blocks, unreachable code
Removes unused imports, functions, variables, commented blocks, and unreachable code safely.
/plugin marketplace add Shavakan/claude-marketplace/plugin install shavakan-commands@shavakanFind and safely remove unused code: imports, functions, variables, commented blocks, unreachable code, unused files.
Safety requirements:
Run prerequisite check:
PLUGIN_ROOT="$HOME/.claude/plugins/marketplaces/shavakan"
if [[ ! "$PLUGIN_ROOT" =~ ^"$HOME"/.* ]]; then
echo "ERROR: Invalid plugin root path"
exit 1
fi
PREREQ_SCRIPT="$PLUGIN_ROOT/commands/cleanup/scripts/check-prerequisites.sh"
if [[ ! -f "$PREREQ_SCRIPT" ]]; then
echo "ERROR: Prerequisites script not found"
exit 1
fi
PREREQ_OUTPUT=$(mktemp)
if "$PREREQ_SCRIPT" > "$PREREQ_OUTPUT" 2>&1; then
source "$PREREQ_OUTPUT"
rm "$PREREQ_OUTPUT"
else
cat "$PREREQ_OUTPUT"
rm "$PREREQ_OUTPUT"
exit 1
fi
This exports: TEST_CMD, BACKUP_BRANCH, LOG_FILE
$ARGUMENTS
Identify and remove dead code that clutters the codebase without affecting functionality.
Unused imports - Modules imported but never referenced
Unused functions - Defined but never called, private methods with no references, exported with no external usage
Unused variables - Declared but never read, parameters never used, constants not referenced
Commented code - 5+ consecutive commented lines with code syntax (belongs in git history)
Unreachable code - After return/throw/break, conditional branches that never execute
Unused files - Source files with no imports, test files for deleted features, replaced implementations
Scan codebase for unused code using language-appropriate static analysis. For each category:
Be cautious with:
Gate: User must review findings before removal.
Present findings to user with risk assessment:
Remove dead code?
□ Safe - Unused imports + variables (auto-fixable)
□ Low risk - Unused private functions + commented code
□ Needs review - Unused public functions + unused files
□ Custom - Select specific items
□ Cancel
Gate: Get user approval on which categories to remove.
For each approved category:
Remove items using language-specific tools when available (eslint --fix, autoflake). For functions/files, manual removal with careful validation.
Test and commit: Run $TEST_CMD after each category. If passing, commit. If failing, rollback and report which removal broke tests.
Continue safely: Process one category at a time. Stop on test failure.
Gate: Tests must pass before moving to next category.
Summarize: items removed per category, files affected, lines eliminated, code coverage maintained.
Delete the backup branch after successful completion:
git branch -D "$BACKUP_BRANCH"
CRITICAL:
git rm for file removal to preserve historyIf tests fail: Rollback, identify which specific removal caused failure, decide whether to skip that item or investigate why it was actually needed.
Review with code-reviewer agent before pushing:
Use shavakan-agents:code-reviewer to verify removals don't introduce issues.
/shavakan-commands:cleanup - Full repository audit/shavakan-commands:cleanup-comments - Clean up comments/shavakan-commands:cleanup-architecture - Refactor structure