charmbracelet/crush · error

failed to delete session: %w

Error message

failed to delete session: %w

What it means

runSessionDelete resolves the target session then calls sessions.Delete. If the DB delete (including cascading message removal) fails, the error is wrapped as 'failed to delete session'. Note: a non-existent ID fails earlier in resolveSessionID, so this is almost always a storage-layer failure.

Source

Thrown at internal/cmd/session.go:305

func runSessionDelete(cmd *cobra.Command, args []string) error {
	event.SetNonInteractive(true)

	ctx, svc, cleanup, err := sessionSetup(cmd)
	if err != nil {
		return err
	}
	defer cleanup()

	event.SessionDeletedCommand(sessionDeleteJSON)

	sess, err := resolveSessionID(ctx, svc.sessions, args[0])
	if err != nil {
		return err
	}

	if err := svc.sessions.Delete(ctx, sess.ID); err != nil {
		return fmt.Errorf("failed to delete session: %w", err)
	}

	out := cmd.OutOrStdout()
	if sessionDeleteJSON {
		enc := json.NewEncoder(out)
		enc.SetEscapeHTML(false)
		return enc.Encode(sessionMutationResult{
			ID:      session.HashID(sess.ID),
			UUID:    sess.ID,
			Title:   sess.Title,
			Deleted: true,
		})
	}

	fmt.Fprintf(out, "Deleted session %s\n", session.HashID(sess.ID)[:12])
	return nil
}

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Ensure no other crush process is running (write lock) and retry
  2. Check the data dir/filesystem is writable and has free disk space
  3. Inspect the wrapped error chain for the exact SQL error
  4. As a last resort back up and repair/recreate the database
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-check write access to the DB file before deleting
dbPath := filepath.Join(dataDir, "crush.db")
if f, err := os.OpenFile(dbPath, os.O_WRONLY, 0); err != nil {
    return fmt.Errorf("database not writable: %w", err)
} else {
    f.Close()
}

Try / catch

if err := svc.sessions.Delete(ctx, sess.ID); err != nil {
    slog.Error("Delete failed", "session", sess.ID, "error", err)
    return fmt.Errorf("failed to delete session: %w", err)
}

Prevention

When it happens

Trigger: svc.sessions.Delete(ctx, sess.ID) errors: SQL failure, foreign-key/constraint issue, DB locked by another process, corrupted DB, or canceled context mid-transaction.

Common situations: SQLite write lock held by a running crush session; read-only filesystem or data dir; disk full preventing the transaction from committing.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/10c0e9b88d5d606f. Report an issue: GitHub.