Andrea Ceruti
03/11/2025, 11:15 AMMarvin
03/11/2025, 11:16 AMdelete
function from the prefect.cli.deployment
module. Here's a concise way to do it:
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.Andrea Ceruti
03/11/2025, 1:56 PMMarvin
03/11/2025, 1:56 PMdelete_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.Andrea Ceruti
03/11/2025, 2:17 PMMarvin
03/11/2025, 2:18 PMread_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`:
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.Andrea Ceruti
03/11/2025, 2:19 PMMarvin
03/11/2025, 2:19 PMbash
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.Andrea Ceruti
03/11/2025, 2:35 PMasync 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))
Marvin
03/11/2025, 2:35 PMdelete_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.Andrea Ceruti
03/11/2025, 2:37 PM# 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,
)
Marvin
03/11/2025, 2:38 PMdeploy
method again with updated values. Here's a method to update an existing deployment:
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.Andrea Ceruti
03/11/2025, 2:39 PMMarvin
03/11/2025, 2:40 PMupdate
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.Andrea Ceruti
03/11/2025, 4:37 PMMarvin
03/11/2025, 4:37 PM<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.