Skip to main content

sonar_cleanup

Clean up unused SonarQube projects and expired tokens.

Description

Identifies and removes stale SonarQube projects and tokens that haven't been used recently. Helps maintain a clean SonarQube environment and free up resources.

Parameters

ParameterTypeRequiredDefaultDescription
olderThanDaysnumberNo30Remove items older than N days
dryRunbooleanNofalsePreview without deleting

Usage

Preview Cleanup

What old projects can be cleaned up?

Tool call:

{
"dryRun": true
}

Execute Cleanup

Clean up old SonarQube projects

Tool call:

{
"dryRun": false
}

Custom Age Threshold

Remove projects not used in 90 days

Tool call:

{
"olderThanDays": 90,
"dryRun": false
}

Response Format

Dry Run (Preview)

CLEANUP PREVIEW (DRY RUN)
━━━━━━━━━━━━━━━━━━━━━━━━━

⚠️ No changes will be made

📊 PROJECTS TO REMOVE (older than 30 days):

┌─────────────────────────────────────────┬───────────────┬────────────┐
│ Project Key │ Last Analysis │ Days Old │
├─────────────────────────────────────────┼───────────────┼────────────┤
│ old-test-project-abc123 │ 2023-10-15 │ 92 │
│ deprecated-api-def456 │ 2023-11-01 │ 76 │
│ temp-experiment-ghi789 │ 2023-12-01 │ 46 │
└─────────────────────────────────────────┴───────────────┴────────────┘

📋 TOKENS TO REVOKE:

┌─────────────────────────────────────────┬───────────────┬────────────┐
│ Token Name │ Last Used │ Days Old │
├─────────────────────────────────────────┼───────────────┼────────────┤
│ old-test-project-token │ 2023-10-15 │ 92 │
│ deprecated-api-token │ 2023-11-01 │ 76 │
└─────────────────────────────────────────┴───────────────┴────────────┘

SUMMARY:
Projects to delete: 3
Tokens to revoke: 2
Space to free: ~15 MB

To execute cleanup, run again with:
{ "dryRun": false }

Actual Cleanup

CLEANUP COMPLETE
━━━━━━━━━━━━━━━━

✅ Cleanup executed successfully

📊 PROJECTS REMOVED:

✓ old-test-project-abc123
✓ deprecated-api-def456
✓ temp-experiment-ghi789

📋 TOKENS REVOKED:

✓ old-test-project-token
✓ deprecated-api-token

SUMMARY:
Projects deleted: 3
Tokens revoked: 2
Space freed: ~15 MB

⚠️ These actions cannot be undone.
Recreate projects with sonar_auto_setup if needed.

Nothing to Clean

CLEANUP STATUS
━━━━━━━━━━━━━━

✅ Nothing to clean up

All projects and tokens are within the 30-day threshold.

Current status:
Active projects: 5
Valid tokens: 8

Oldest project: myapp-a1b2c3 (15 days ago)

Age Calculation

ItemAge Based On
ProjectsLast successful analysis
TokensLast usage for API call

Examples

Example 1: Regular Maintenance

Prompt:

Clean up old projects from last month

Tool Call:

{
"olderThanDays": 30,
"dryRun": false
}

Example 2: Conservative Cleanup

Prompt:

Remove only very old projects (3+ months)

Tool Call:

{
"olderThanDays": 90,
"dryRun": false
}

Example 3: Safety Preview

Prompt:

Show me what would be cleaned without doing it

Tool Call:

{
"olderThanDays": 30,
"dryRun": true
}

What Gets Cleaned

Projects

  • Projects created by Bob the Fixer
  • No analysis in X days
  • Associated data and history

Tokens

  • Tokens created by Bob the Fixer
  • Not used in X days
  • Project analysis tokens

What's NOT Cleaned

  • Projects created manually in SonarQube
  • System/admin tokens
  • Projects with recent activity

Safety Features

FeatureDescription
Dry run defaultPreview before deleting
Bob-only cleanupOnly cleans Bob-created items
Activity checkVerifies no recent usage
ConfirmationReports what was deleted

Best Practices

  1. Always dry run first - Preview before executing
  2. Regular cleanup - Run monthly or quarterly
  3. Reasonable threshold - 30-60 days is typical
  4. Check before demos - Don't delete active projects

Common Issues

"No projects found"

Cause: No Bob-created projects exist.

Solution: This is normal for fresh installations.

"Permission denied"

Cause: Token lacks delete permissions.

Solution: Run sonar_diagnose_permissions to check.

"Some projects not deleted"

Cause: Projects have recent activity or were created externally.

Solution: Use SonarQube UI for manual cleanup.

Automation

For CI/CD cleanup:

# Example: Weekly cleanup job
cleanup:
schedule: "0 0 * * 0" # Weekly on Sunday
script:
- sonar_cleanup --older-than-days 60