<@ULVA73B9P> for a flow that i started through the...
# ask-marvin
r
@Marvin for a flow that i started through the python client via
client.create_flow_run_from_deployment
, how do I cancel the run? I don’t want to delete it, just cancel via the python api
m
To cancel a flow run using the Prefect Python client, you can use the
PrefectClient
class from the
prefect.client.orchestration
module. Here's a concise example of how you might achieve this:
Copy code
python
from prefect.client.orchestration import get_client
from prefect.states import Cancelled

async def cancel_flow_run(flow_run_id: str):
    async with get_client() as client:
        await client.set_flow_run_state(flow_run_id=flow_run_id, state=Cancelled())

# Example usage
import asyncio

flow_run_id = "your-flow-run-id"
asyncio.run(cancel_flow_run(flow_run_id))
This script uses the
get_client()
function to interact with the Prefect API and sets the state of the specified flow run to
Cancelled
. Remember to replace
"your-flow-run-id"
with the actual ID of the flow run you wish to cancel. For more details on the cancellation process and potential issues, you can refer to the Prefect documentation on cancellation.