How to use Prefect Client to delete all deployments?

Some users like to operate in a Terraform-style and have each CI/CD workflow deleting all existing flow deployments and creating new ones.

Here is how you can delete all deployments existing in your workspace (note, this is irreversible :warning: ):

import asyncio
from prefect.client import get_client


async def remove_all_deployments():
    client = get_client()
    deployments = await client.read_deployments()
    for deployment in deployments:
        print(f"Deleting deployment: {deployment.name}")
        await client.delete_deployment(deployment.id)
        print(f"Deployment with UUID {deployment.id} deleted")


if __name__ == "__main__":
    asyncio.run(remove_all_deployments())

Once all deployments are deleted, you can start with a clean slate and start creating new ones following:

1 Like