what is the difference between cancel and delete r...
# ask-community
g
what is the difference between cancel and delete regarding a flow run? If I delete a flow run would this be cancelled also? I’m asking because I do not see any cancel_flow_run method on get_client()
1
d
M working on trying to cancel the flow run myself 😄
👋 1
client.set_flow_run_state
m looking into this
Copy code
from prefect.client.schemas.filters import FlowRunFilterState
        from prefect.states import Cancelling
        running_flows=await client.read_flow_runs(
            flow_run_filter=FlowRunFilter(state=FlowRunFilterState(name={"any_":["Running"]}))
        )
        for running_flow in running_flows:
            print(running_flow.id,running_flow.name)
            res=await client.set_flow_run_state(running_flow.id,Cancelling())
            print(res)
Here you go
g
I think it is really difficult to know the outcome. What I’m afraid it to end up with a method which sets a state but actually still running in the agent
d
#YOLO
g
do you think it works?
d
M cancelling all the running tasks
g
they are not running in the agent, aren’t they?
d
kubernetes
prefecet agent yes
the number of pods are going down
🙌 1
g
no, what I mean, is that code actually cancelling the run or it is just to set the flow run state but the flow is still running underneath?
that’s a good sign
thanks for the code snippet!
d
wait
it didnt do anything 😄
g
LOL
d
the state is in cancelling
g
but still running?
d
I guess cuz the flows are waiting for a task run concurrency lock
maybe they will start cancelling in a while
actaully m not sure 😄
ill just wait like 10 mins and see if the number of pods go down
g
😄 maybe in the next checkpoint during the flow lifecycle will stop
d
let me know if you have some finiding on ur end
yes thats what I am hoping
g
sure, although would be good to have it checked by some Prefect developers
many time I’ve used the ui to cancel a flow run and actually the state never change to cancelled, always stay in cancelling
@Deceivious do you know how to get the current deployment id?
I thought from the context but I don’t see any method for the purpose
d
From the from run I'd filter
?
g
you mean read_deployments and then filter?
Copy code
read_deployment_by_name
d
Copy code
client.read_flow_runs
has deployment_id
g
yeah, I wanted to exclude current flow run from the flow runs I want to cancel
Copy code
FlowRunContext.get().flow_run.deployment_id
does the trick
j
Deleting and cancelling a flow run are two different things.
CANCELLED
is a flow run state, the same way
RUNNING
and
COMPLETED
are states. The client uses
/set_state
to a propose a cancelled state, just like any other state. This is the graceful way to stop a flow run in progress, especially if your flow run is executing on remote infrastructure. Deleting a flow run that is in progress is equivalent to yanking the cord out the machine in the middle. It would stop but not gracefully
d
Thanks @Jake Kaplan. Was my code to chance all the running flows to Cancelling correct?
g
@Jake Kaplan thanks for you help! What if we use set_flow_run_state with Cancelling? Is it wrong then? Should we use cancelled? With force=True?
j
Heres an example from the CLI of how to cancel a flow run: https://github.com/PrefectHQ/prefect/blob/main/src/prefect/cli/flow_run.py#L124-L142
CANCELLING
is the right state to pass, which indicates that the flow run should be prepared to be
CANCELLED
🙌 1
you shouldn't need
Force=True
, the code snippet above looks right at glance to cancel running flow runs
g
thanks! What is it force parameter for?
when to use it?
d
If the Flows are being blocked by a task run concurrency tag , does the flow need to await to acquire the "tag lock" before it can cancels itself?
j
task run concurrency is purely for the running of tasks and shouldn't affect anything related to being able to change the flow run state
force=True
changes a state but tells the API to disregard most orchestration rules. I generally would not use it, it'll break expectations of what is "supposed to happen"
🙌 1
d
Weird! When I use the code to set the running flows to Cancelling state, nothing really happens. But when I do it from UI it works