Add bd cleanup command for bulk deletion of closed issues

Implements bd-jx90: simple cleanup command without API key requirement.

Features:
- Delete all closed issues with --force
- Filter by age with --older-than N (days)
- Preview with --dry-run
- Cascade deletion with --cascade
- JSON output support

Usage:
  bd cleanup --force                    # Delete all closed
  bd cleanup --older-than 30 --force    # Delete >30 days old
  bd cleanup --dry-run                  # Preview

Closes bd-jx90

Amp-Thread-ID: https://ampcode.com/threads/T-8d905db0-4ec7-411e-95de-1f044219dc9c
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-11-07 22:06:50 -08:00
parent de665f2341
commit 1065e66a75
3 changed files with 142 additions and 1 deletions

View File

@@ -268,7 +268,7 @@
{"id":"bd-j5aj","content_hash":"2236f911e6f321a74aa61bdf702d24949e44a68ed511d12dd011aa4103c89230","title":"Issue 2","description":"","status":"closed","priority":1,"issue_type":"task","created_at":"2025-11-07T19:07:16.364549-08:00","updated_at":"2025-11-07T21:55:09.43066-08:00","closed_at":"2025-11-07T21:55:09.43066-08:00","source_repo":"."}
{"id":"bd-j7e2","content_hash":"aa810a603c630b2435f7a75d4c38a57b72e557e64913c1b701ccda87109f2ffe","title":"RPC diagnostics: BD_RPC_DEBUG timing logs","description":"Add lightweight diagnostic logging for RPC connection attempts:\n- BD_RPC_DEBUG=1 prints to stderr:\n - Socket path being dialed\n - Socket exists check result \n - Dial start/stop time\n - Connection outcome\n- Improve bd daemon --status messaging when lock not held\n\nThis helps field triage of connection issues without verbose daemon logs.","status":"closed","priority":3,"issue_type":"task","created_at":"2025-11-07T16:42:12.772364-08:00","updated_at":"2025-11-07T21:29:32.243458-08:00","closed_at":"2025-11-07T21:29:32.243458-08:00","source_repo":".","dependencies":[{"issue_id":"bd-j7e2","depends_on_id":"bd-ndyz","type":"discovered-from","created_at":"2025-11-07T16:42:12.773714-08:00","created_by":"daemon"}]}
{"id":"bd-jpm9","content_hash":"cdd43e0460cfe2e1c0f49728248d4bb441f5c6b17943dd9d13efe32de3e42147","title":"Issue to close","description":"","status":"closed","priority":1,"issue_type":"bug","created_at":"2025-11-07T19:07:13.57982-08:00","updated_at":"2025-11-07T19:07:13.602394-08:00","closed_at":"2025-11-07T19:07:13.602394-08:00","source_repo":"."}
{"id":"bd-jx90","content_hash":"5e08ff79669eaf606022b1ab13a167c0689e9d9a1b2f3bb4fb880ca792546411","title":"Add simple cleanup command to delete closed issues","description":"Users want a simple command to delete all closed issues without requiring Anthropic API key (unlike compact). Requested in GH #243.","status":"open","priority":2,"issue_type":"feature","created_at":"2025-11-07T00:26:30.372137-08:00","updated_at":"2025-11-07T00:26:30.372137-08:00","source_repo":"."}
{"id":"bd-jx90","content_hash":"e70db128e4ce123de9961ce554c2f67c565ac8d0bce7cf056213279993e48b25","title":"Add simple cleanup command to delete closed issues","description":"Users want a simple command to delete all closed issues without requiring Anthropic API key (unlike compact). Requested in GH #243.","status":"closed","priority":2,"issue_type":"feature","created_at":"2025-11-07T00:26:30.372137-08:00","updated_at":"2025-11-07T22:05:16.325863-08:00","closed_at":"2025-11-07T22:05:16.325863-08:00","source_repo":"."}
{"id":"bd-k0j9","content_hash":"52d1e6f87bd7655018bd89dbbbaf8da66bdcba45de6138fd237810365a04606a","title":"Test dependency parent","description":"","status":"closed","priority":2,"issue_type":"task","created_at":"2025-11-05T11:23:02.505901-08:00","updated_at":"2025-11-05T11:23:20.91305-08:00","closed_at":"2025-11-05T11:23:20.91305-08:00","source_repo":"."}
{"id":"bd-k58","content_hash":"cc90fb20e7bd178b52133d4d0f8781dce2debb46519674ae6356291d597fc13d","title":"Proposal workflow (propose/withdraw/accept)","description":"Implement commands and state machine for moving issues between personal planning repos and canonical upstream repos, enabling contributors to propose work without polluting PRs.","design":"Commands:\n- bd propose \u003cid\u003e [--target \u003crepo\u003e] - Move issue to target repo\n- bd withdraw \u003cid\u003e - Un-propose (move back)\n- bd accept \u003cid\u003e - Maintainer accepts proposal\n\nVisibility states:\n- local: Personal planning only\n- proposed: Staged for upstream PR\n- canonical: Accepted by upstream (default for existing)\n\nOptional visibility field (backward compatible, defaults to canonical)","acceptance_criteria":"1. bd propose moves issue from planning to primary repo\n2. bd withdraw reverses proposal\n3. bd accept (maintainer) finalizes acceptance\n4. Visibility field tracks state (local/proposed/canonical)\n5. Backward compatible - existing issues default to canonical\n6. State transitions are atomic and git-tracked","status":"closed","priority":1,"issue_type":"epic","created_at":"2025-11-04T11:21:41.113647-08:00","updated_at":"2025-11-05T00:08:38.19821-08:00","closed_at":"2025-11-05T00:08:38.198213-08:00","source_repo":".","dependencies":[{"issue_id":"bd-k58","depends_on_id":"bd-4ms","type":"parent-child","created_at":"2025-11-04T11:22:21.811261-08:00","created_by":"daemon"}]}
{"id":"bd-kazt","content_hash":"a3bd467bc111fa74cf6fc72e2622cc3186f736f6aa25bd4a00a8e256cd042fa6","title":"Add tests for 3-way merge scenarios","description":"Comprehensive test coverage for merge logic.\n\n**Test cases**:\n- Simple field updates (left vs right)\n- Dependency merging (union + dedup)\n- Timestamp handling (max wins)\n- Deletion detection (deleted in one, modified in other)\n- Conflict generation (incompatible changes)\n- Issue resurrection prevention (bd-hv01 regression test)\n\n**Files**:\n- `internal/merge/merge_test.go`\n- `cmd/bd/merge_test.go`","status":"closed","priority":1,"issue_type":"task","created_at":"2025-11-05T18:42:20.472275-08:00","updated_at":"2025-11-06T15:51:51.365883-08:00","closed_at":"2025-11-06T15:51:51.365883-08:00","source_repo":".","dependencies":[{"issue_id":"bd-kazt","depends_on_id":"bd-qqvw","type":"parent-child","created_at":"2025-11-05T18:42:28.740517-08:00","created_by":"daemon"},{"issue_id":"bd-kazt","depends_on_id":"bd-oif6","type":"blocks","created_at":"2025-11-05T18:42:35.469582-08:00","created_by":"daemon"}]}

View File

@@ -245,6 +245,12 @@ bd close <id> [<id>...] --reason "Done" --json
# Reopen closed issues (supports multiple IDs)
bd reopen <id> [<id>...] --reason "Reopening" --json
# Clean up closed issues (bulk deletion)
bd cleanup --force --json # Delete ALL closed issues
bd cleanup --older-than 30 --force --json # Delete closed >30 days ago
bd cleanup --dry-run --json # Preview what would be deleted
bd cleanup --older-than 90 --cascade --force --json # Delete old + dependents
# Show dependency tree
bd dep tree <id>

135
cmd/bd/cleanup.go Normal file
View File

@@ -0,0 +1,135 @@
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/types"
)
var cleanupCmd = &cobra.Command{
Use: "cleanup",
Short: "Delete all closed issues (optionally filtered by age)",
Long: `Delete all closed issues to clean up the database.
By default, deletes ALL closed issues. Use --older-than to only delete
issues closed before a certain date.
EXAMPLES:
Delete all closed issues:
bd cleanup --force
Delete issues closed more than 30 days ago:
bd cleanup --older-than 30 --force
Preview what would be deleted:
bd cleanup --dry-run
bd cleanup --older-than 90 --dry-run
SAFETY:
- Requires --force flag to actually delete (unless --dry-run)
- Supports --cascade to delete dependents
- Shows preview of what will be deleted
- Use --json for programmatic output`,
Run: func(cmd *cobra.Command, args []string) {
force, _ := cmd.Flags().GetBool("force")
dryRun, _ := cmd.Flags().GetBool("dry-run")
cascade, _ := cmd.Flags().GetBool("cascade")
olderThanDays, _ := cmd.Flags().GetInt("older-than")
// Ensure we have storage
if daemonClient != nil {
if err := ensureDirectMode("daemon does not support delete command"); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
} else if store == nil {
if err := ensureStoreActive(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
ctx := context.Background()
// Build filter for closed issues
statusClosed := types.StatusClosed
filter := types.IssueFilter{
Status: &statusClosed,
}
// Add age filter if specified
if olderThanDays > 0 {
cutoffTime := time.Now().AddDate(0, 0, -olderThanDays)
filter.ClosedBefore = &cutoffTime
}
// Get all closed issues matching filter
closedIssues, err := store.SearchIssues(ctx, "", filter)
if err != nil {
fmt.Fprintf(os.Stderr, "Error listing issues: %v\n", err)
os.Exit(1)
}
if len(closedIssues) == 0 {
if jsonOutput {
result := map[string]interface{}{
"deleted_count": 0,
"message": "No closed issues to delete",
}
if olderThanDays > 0 {
result["filter"] = fmt.Sprintf("older than %d days", olderThanDays)
}
output, _ := json.MarshalIndent(result, "", " ")
fmt.Println(string(output))
} else {
msg := "No closed issues to delete"
if olderThanDays > 0 {
msg = fmt.Sprintf("No closed issues older than %d days to delete", olderThanDays)
}
fmt.Println(msg)
}
return
}
// Extract IDs
issueIDs := make([]string, len(closedIssues))
for i, issue := range closedIssues {
issueIDs[i] = issue.ID
}
// Show preview
if !force && !dryRun {
fmt.Fprintf(os.Stderr, "Would delete %d closed issue(s). Use --force to confirm or --dry-run to preview.\n", len(issueIDs))
os.Exit(1)
}
if !jsonOutput {
if olderThanDays > 0 {
fmt.Printf("Found %d closed issue(s) older than %d days\n", len(closedIssues), olderThanDays)
} else {
fmt.Printf("Found %d closed issue(s)\n", len(closedIssues))
}
if dryRun {
fmt.Println(color.YellowString("DRY RUN - no changes will be made"))
}
fmt.Println()
}
// Use the existing batch deletion logic
deleteBatch(cmd, issueIDs, force, dryRun, cascade, jsonOutput)
},
}
func init() {
cleanupCmd.Flags().BoolP("force", "f", false, "Actually delete (without this flag, shows error)")
cleanupCmd.Flags().Bool("dry-run", false, "Preview what would be deleted without making changes")
cleanupCmd.Flags().Bool("cascade", false, "Recursively delete all dependent issues")
cleanupCmd.Flags().Int("older-than", 0, "Only delete issues closed more than N days ago (0 = all closed issues)")
rootCmd.AddCommand(cleanupCmd)
}