Thread
#prefect-community
    z

    Zach

    1 year ago
    I want to cancel every currently scheduled run for one of my flows basically
    nicholas

    nicholas

    1 year ago
    Hi @Zach - I think the most straightforward way to do this (assuming these were programmatically-created runs and not auto-scheduled runs) would be to use the python graphql client with a query like this:
    query {
      flow_run(where: { flow_id: { _eq: "<<flow_id>>" }, state: { _eq: "Scheduled" } } ) {
        id
      }
    }
    and then map over that list something like this:
    mutation_contents = ""
    
    for i, id in enumerate(flow_run_ids):
      mutation_contents += f"""
        cancel_{i}: cancel_flow_run(input: { flow_run_id: {id} }) {
          state
        }
      """
    
    mutation = f"""
      mutation {
        {mutation_contents}
      }
    """
    Oops, the client also exposes that mutation so you could make that even more straightforward:
    from prefect.client import Client
    
    client = Client()
    
    for id in flow_run_ids:
      client.cancel_flow_run(id)