<@ULVA73B9P> please write a prefect flow that dele...
# ask-marvin
d
@Marvin please write a prefect flow that deletes all task runs that are in a completed state and are greater than 3 a day old.
m
I couldn't find exact documentation for deleting task runs, but here's a basic outline to get you started. This example utilizes Prefect's ability to interact with its API to fetch and delete task runs. 1. Install Prefect: Ensure you have Prefect installed.
Copy code
bash
pip install prefect
2. Prefect Flow for Deleting Completed Task Runs:
Copy code
python
from prefect import flow, task, get_client
from datetime import datetime, timedelta

@task
async def delete_old_task_runs():
    async with get_client() as client:
        # Calculate the cutoff datetime
        cutoff = datetime.utcnow() - timedelta(days=3)

        # Fetch all task runs in completed state older than cutoff
        task_runs = await client.read_task_runs(where={
            "state": {"type": "COMPLETED"},
            "end_time": {"lt": cutoff.isoformat()}
        })

        # Delete the task runs
        for task_run in task_runs:
            await client.delete_task_run(task_run.id)

@flow(log_prints=True)
async def cleanup_flow():
    await delete_old_task_runs()

if __name__ == "__main__":
    cleanup_flow()
3. Run the Flow: You can run the flow manually:
Copy code
bash
python your_script_name.py
Make sure to replace
"your_script_name.py"
with the actual script file name. Feel free to review the details and make necessary customizations according to your Prefect version and setup. For any further details, you might want to refer to the Prefect documentation. If you need more specific examples, you can explore Prefect code examples.
n
almost-ish? instead of
where
you'd use
FlowRunFilter(state=dict(type=dict(any_=["COMPLETED"])), start_time=...)
d
Thank you, which package is FlowRunFilter located in ?
n
from prefect.client.schemas.filters import FlowRunFilter
fwiw: to answer "how to import X?" in general i'd suggest • do a github search like this • use
client
schemas, not
server
schemas (unless you're contributing a server component šŸ™‚ )
āœ… 1
d
Thank you