How can I cancel, kill, remove, (whatever you want...
# ask-community
j
How can I cancel, kill, remove, (whatever you want to call it) all flow runs in Prefect Cloud? Do I need to use some CLI options or run a script? I have an issue where I’m hitting API limits due to the system having gotten bogged down when there was an error present in a flow. I have killed the agent and restarted it, but all the flow runs are still trying to execute, and there’s no easy option in Prefect Cloud to clear them.
1
So I found this through searching: https://discourse.prefect.io/t/deleting-all-flow-runs-using-cli/2982 … however, it mentions if you have a lot of flow runs then it would be faster to do using a client. There’s no example client code given for doing such maintenance tasks. Nor does it indicate what “lot” means. Is that hundreds, thousands, hundreds of thousands, etc.
After correcting the command in that post to
Copy code
prefect flow-run ls | awk 'NR>3 {print $2}' | while read line
do
    prefect flow-run delete $line
done
I have figured out that “lot” means more than “tens” of flow runs… I have thousands of flow runs that need to be removed to clean up this mess.
j
hey, for deleting runs something like this should work. Be aware that without passing a filter to
read_flow_runs
this will delete runs indiscriminately
Copy code
import asyncio
from prefect.client.orchestration import get_client


async def delete_flow_runs():
    async with get_client() as client:
        while True:
            flow_runs = await client.read_flow_runs(limit=50)

            if len(flow_runs) == 0:
                break

            await asyncio.gather(*[client.delete_flow_run(fr.id) for fr in flow_runs])


if __name__ == '__main__':
    asyncio.run(delete_flow_runs())
If you give a little more context on the flow runs you're trying to delete, there also may be another mechanism. For example if it's a lot of scheduled runs, toggling the schedule should clear them
j
Jake, thanks! I’ll try running that later. I think I’m going to have to remove all flow runs, deployments, and pretty much start over…
j
Is this in Cloud or self hosted?
j
How do you mean toggling the schedule?
j
Sorry I meant on the deployment. Setting the deployment inactive OR updating/deleting the schedule on a flow run should clear all the FUTURE scheduled runs
j
Cloud… okay… I’ll try that too… because I’m getting duplicate flow runs with only a single schedule… separate issue, but might fix that at the same time…