Skip to content

Add batch delete support for flow runs #19904

@zzstoatzz

Description

@zzstoatzz

Parent Issue

Part of #19903 (CLI improvements for automation and agent usage)

Problem

Currently prefect flow-run delete only accepts a single flow run ID:

# Works
prefect flow-run delete 8d38609e-0a80-4045-bfb1-8f3c07e5cf55

# Doesn't work - have to run 4 separate commands
prefect flow-run delete id1 id2 id3 id4

When cleaning up orphaned or stuck flow runs, this requires running the command multiple times in a loop, which is slow and tedious.

Proposed Solution

1. Accept multiple IDs as positional arguments

prefect flow-run delete <id1> <id2> <id3> <id4>

2. Accept IDs from stdin

# Pipe from another command
prefect flow-run ls --state PENDING -o json | jq -r '.[].id' | prefect flow-run delete --stdin

# Or from a file
cat orphaned-runs.txt | prefect flow-run delete --stdin

3. Query-based delete (stretch goal)

# Delete all pending runs older than 7 days
prefect flow-run delete --state PENDING --older-than 7d

# Delete orphaned runs (no deployment)
prefect flow-run delete --deployment-id null

# Dry run to preview
prefect flow-run delete --state FAILED --dry-run

Use Case

While debugging a Prefect workspace, I found 4 orphaned flow runs from 2 days ago stuck in PENDING state with deployment: null (their deployments had been deleted). Cleaning these up required:

prefect flow-run delete 8d38609e-0a80-4045-bfb1-8f3c07e5cf55
prefect flow-run delete 3b7ce29f-64cf-4d1b-ad16-cff9cac1ce61  
prefect flow-run delete ecfbecf9-4841-4320-980f-3405bc11b398
prefect flow-run delete 1b230e8e-69e9-4db0-b169-838a00d74db6

With batch support, this becomes:

prefect flow-run delete 8d38609e-0a80-4045-bfb1-8f3c07e5cf55 3b7ce29f-64cf-4d1b-ad16-cff9cac1ce61 ecfbecf9-4841-4320-980f-3405bc11b398 1b230e8e-69e9-4db0-b169-838a00d74db6

Additional Notes

  • Should also apply to prefect flow-run cancel
  • Consider similar patterns for other resources (deployments, blocks, etc.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    cliRelated to the Prefect CLIenhancementAn improvement of an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions