<@ULVA73B9P> in prefect 3.x how do I delete a depl...
# ask-marvin
a
@Marvin in prefect 3.x how do I delete a deployment from Python sdk having its deployment name?
m
To delete a deployment in Prefect 3.x using the Python SDK, you can utilize the
delete
function from the
prefect.cli.deployment
module. Here's a concise way to do it:
Copy code
python
from prefect.cli.deployment import delete

# Replace 'deployment_name' with the actual name of your deployment
delete('deployment_name')
Ensure that you have the necessary permissions to delete the deployment. The function will handle the removal of the specified deployment by its name. For further details, you can explore the Prefect documentation or refer to the Prefect CLI Deployment Module.
a
@Marvin Ok so what about this function? from prefect import get_client async def delete_deployment_by_name(flow_name, deployment_name): async with get_client() as client: # Fetch deployments matching the name deployments = await client.read_deployments( flow_name=flow_name, deployment_name=deployment_name ) if not deployments: print(f"No deployment found with name: {flow_name}/{deployment_name}") return # Assuming the first match is the one we want deployment_id = deployments[0].id # Delete the deployment await client.delete_deployment(deployment_id) print(f"Deployment {flow_name}/{deployment_name} deleted successfully") # Usage import asyncio asyncio.run(delete_deployment_by_name("your_flow_name", "your_deployment_name"))
m
Your function
delete_deployment_by_name
looks correct for deleting a deployment using the Prefect 3.x Python SDK. Here's a quick breakdown of how it works: 1. Get Client: It uses
get_client
to connect to the Prefect API. 2. Read Deployments: It fetches deployments using
client.read_deployments
by matching the
flow_name
and
deployment_name
. 3. Delete Deployment: If a matching deployment is found, it deletes the deployment using
client.delete_deployment
. This function is asynchronous, so make sure to run it within an async context using
asyncio.run()
. Everything seems in order with the imports and functionality. You can proceed with this approach for managing deployments programmatically.
a
@Marvin I had warnings though on client.read_deployments since I see Unexpected argument from the linter. Can you re elaborate the method such that it works with the complete name ( flow_name/deployment_name )? Or how can I do it
m
It seems that the
read_deployments
method might not accept
flow_name
and
deployment_name
as direct arguments. Instead, you can use the
DeploymentAsyncClient
to interact with deployments. Here's a revised approach to delete a deployment using the complete name `flow_name/deployment_name`:
Copy code
python
from prefect.client.orchestration import PrefectClient

async def delete_deployment_by_full_name(full_name):
    async with PrefectClient() as client:
        # Fetch deployments using the full name
        deployment = await client.read_deployment_by_name(full_name)

        if not deployment:
            print(f"No deployment found with name: {full_name}")
            return

        # Delete the deployment
        await client.delete_deployment(deployment.id)
        print(f"Deployment {full_name} deleted successfully")

# Usage
import asyncio
asyncio.run(delete_deployment_by_full_name("your_flow_name/your_deployment_name"))
This code assumes that
read_deployment_by_name
is a valid method, which is typical for such client interfaces. If you encounter issues, you might need to refer to the specific client methods available within
DeploymentAsyncClient
for more precise handling.
a
@Marvin isn't there a more straightfoward and simpler methods for deleting deployments by the name?
m
Unfortunately, the Prefect 3.x documentation does not provide a straightforward method to delete deployments by name directly using the Python SDK. The approach typically involves fetching the deployment details first and then deleting it using its ID. This is due to the way deployment identification is structured within Prefect. However, if you want a simpler method, you might consider using the Prefect CLI, which allows you to manage deployments more directly. For example:
Copy code
bash
prefect deployment delete <flow_name>/<deployment_name>
This CLI command deletes the specified deployment directly by its full name. For programmatic usage, the method I previously provided using the
PrefectClient
is the standard way to achieve this through the SDK, albeit with a bit more complexity due to the need to handle deployment retrieval and deletion separately.
a
@Marvin I will try and test this code logic
Copy code
async def delete_deployment_by_name(full_name: str):

    async with get_client() as client:
        try:
            deployment = await client.read_deployment_by_name(full_name)
        except ObjectNotFound:
            print(f"No deployment found with name: {full_name}")
            return
        # Delete the deployment
        await client.delete_deployment(deployment.id)
        print(f"Deployment {full_name} deleted successfully")


def delete_prefect_schedule(deployment_name: str):
    """
        Deletes a deployment given the deployment name (the flow name is fixed for now).
        E.g. delete_prefect_schedule("my-deployment") with flow_name = "my_flow"
             -> deletes the "my-flow/my-deployment" deployment.
    """
    flow_name = "generate_report_flow"
    full_name = f"{flow_name}/{deployment_name}"
    asyncio.run(delete_deployment_by_name(full_name))
m
Your code logic looks sound for deleting a deployment by its full name in Prefect 3.x. Here's a quick breakdown: 1. Async Function:
delete_deployment_by_name
uses
get_client
to asynchronously connect to the Prefect API and attempts to read the deployment by its full name. If not found, it catches the
ObjectNotFound
exception. 2. Delete Deployment: If the deployment is found, it deletes it using
client.delete_deployment
. 3. Wrapper Function:
delete_prefect_schedule
constructs the full name using a fixed flow name and calls the async function to perform the deletion. This approach should work effectively for your needs. Just ensure that your environment is set up to handle async operations and that you have the necessary permissions to delete deployments.
a
@Marvin What can I do instead when I want to deploy a deployes flow? For example I want to modify some of its parameters or its schedule. This is the code with which I deploy flow, create a method for the update
Copy code
# Parameters for the flow; these will be passed when the flow runs.
parameters = {
    "report_id": scheduling_resource.report_id,
    "recipients": scheduling_resource.recipients,
    "output_format": scheduling_resource.output_format,
    "user_external_id": user_external_id,
    "role_name": role_name,
    "client": client,
}

# Build a unique deployment name.
deployment_name = f"report:{scheduling_resource.report_id}_scheduling:{scheduling_resource.scheduling_id}"
flow.from_source(source=str(Path(__file__).parent), entrypoint="flow_module.py:generate_report_flow").deploy(
    name=deployment_name,
    work_pool_name="dynamic-report-scheduling-pool",
    cron=cron_expression,
    parameters=parameters,
    image="<http://857010180973.dkr.ecr.eu-central-1.amazonaws.com/prefect:0.0.4|857010180973.dkr.ecr.eu-central-1.amazonaws.com/prefect:0.0.4>",
    build=False,
    push=False,
)
m
To update a deployed flow's parameters or schedule in Prefect 3.x, you can modify the deployment using the
deploy
method again with updated values. Here's a method to update an existing deployment:
Copy code
python
from prefect import flow

async def update_deployment(deployment_name, cron_expression, new_parameters):
    # Assuming 'generate_report_flow' is the flow you want to update
    flow.from_source(source=str(Path(__file__).parent), entrypoint="flow_module.py:generate_report_flow").deploy(
        name=deployment_name,
        work_pool_name="dynamic-report-scheduling-pool",
        cron=cron_expression,
        parameters=new_parameters,
        image="<http://857010180973.dkr.ecr.eu-central-1.amazonaws.com/prefect:0.0.4|857010180973.dkr.ecr.eu-central-1.amazonaws.com/prefect:0.0.4>",
        build=False,
        push=False,
    )

# Usage
new_parameters = {
    "report_id": "new_report_id",
    "recipients": ["<mailto:new_recipient@example.com|new_recipient@example.com>"],
    "output_format": "pdf",
    "user_external_id": "new_user_id",
    "role_name": "new_role_name",
    "client": "new_client",
}

update_deployment("report:new_report_id_scheduling:new_scheduling_id", "0 9 * * *", new_parameters)
### Key Points: - Re-deploy: You use the same
deploy
method to update parameters or schedules. Prefect will handle the update if the deployment name is the same. - Parameters and Schedule: Simply pass the new parameters and cron expression to update the deployment's configuration. - Immutability: Prefect treats deployments as immutable, so re-deploying with the same name effectively updates it. This approach allows you to programmatically manage and update deployments as needed.
a
@Marvin I heard that there is an update parameter into deploy. Is it still needed or I can just call it again with the new parameters?
m
The Prefect 3.x documentation does not explicitly mention an
update
parameter for the
deploy
method. Instead, you can simply call the
deploy
method again with the updated parameters or schedule, and Prefect will handle updating the existing deployment if the deployment name remains the same. This means you can re-deploy the flow with new configurations without needing a specific update flag. Just ensure that the deployment name is consistent, and Prefect will recognize it as an update to the existing deployment.
a
@Marvin I can't find my deployment even if scheduled, what can I do? The docs of prefect 3.x says: prefect.client.orchestration._deployments.client.DeploymentAsyncClient async def read_deployment_by_name(self, name: str) -> Coroutine[Any, Any, DeploymentResponse] Query the Prefect API for a deployment by name. Params: name – A deployed flow's name: <FLOW_NAME>/<DEPLOYMENT_NAME> Returns: a Deployment model representation of the deployment Raises: ObjectNotFound – If request returns 404 RequestError – If request fails Can I check somewhere in the Ui where is the complete name of a deployment? I can see the name I am searching for in the deployments, but I am not able to find it with the flow_name in fron
m
To view the complete name of a deployment in the Prefect UI, follow these steps: 1. Navigate to the Deployments Page: In the Prefect UI, go to the Deployments section where all your deployments are listed. 2. Check Deployment Details: Click on the deployment you are interested in. The details page should show both the flow name and the deployment name. 3. Full Name Format: The complete name format is
<FLOW_NAME>/<DEPLOYMENT_NAME>
. Ensure that you are using this format when querying or interacting with deployments programmatically. If you can see the deployment in the UI but are having trouble finding it programmatically, double-check the exact flow and deployment names as they appear in the UI. Any discrepancy in naming, such as case sensitivity or spacing, could lead to issues when querying.