Skip to content

Commit 5a0994f

Browse files
authored
add list_deployed_flows (#168)
1 parent 360dc02 commit 5a0994f

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

docs/metaflow/managing-flows/deployer.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ triggered_run.terminate()
7878

7979

8080
## Accessing Previously Deployed Flows
81+
8182
You can retrieve an existing `deployed_flow` object using the
8283
`from_deployment` method instead of creating a new deployment. This allows
8384
you to work with flows that were previously deployed without having to call
@@ -110,6 +111,52 @@ triggered_run = deployed_flow.trigger()
110111
The `from_deployment` method is only available for argo-workflows at the moment.
111112
:::
112113

114+
## Listing deployed flows
115+
116+
You can list all deployed flows using the `list_deployed_flows` class method. This is useful for discovering existing deployments, performing cleanup operations, etc.
117+
118+
```py
119+
from metaflow import DeployedFlow
120+
121+
# List all deployed flows
122+
for df in DeployedFlow.list_deployed_flows():
123+
print(f"Found deployment: {df.name}")
124+
```
125+
126+
You can also filter by flow name to find all deployments of a specific flow by passing in the `flow_name` parameter.
127+
128+
### Example: Cleaning up old deployments
129+
130+
A common use case is to clean up deployments that haven't been used recently. Here's an example that deletes templates that haven't run in the last 90 days:
131+
132+
```py
133+
from datetime import datetime, timedelta
134+
from metaflow import Flow, DeployedFlow, namespace
135+
136+
# Delete templates that haven't run in the last 90 days
137+
cutoff_date = datetime.now() - timedelta(days=90)
138+
139+
for df in DeployedFlow.list_deployed_flows():
140+
try:
141+
namespace(None)
142+
argo_runs = Flow(df.flow_name).runs("runtime:argo-workflows")
143+
latest_run = next(argo_runs, None)
144+
145+
if latest_run and latest_run.created_at >= cutoff_date:
146+
print(f"Keeping recent template: {df.name} (last run: {latest_run.created_at})")
147+
else:
148+
reason = "no Argo runs" if not latest_run else f"last run: {latest_run.created_at}"
149+
print(f"Deleting old template: {df.name} ({reason})")
150+
df.delete()
151+
152+
except Exception as e:
153+
print(f"Error processing {df.name}: {e}")
154+
```
155+
156+
:::note
157+
The `list_deployed_flows` method is only available for argo-workflows at the moment.
158+
:::
159+
113160
## Orchestrator-specific methods
114161

115162
Besides the common methods highlighted above, each orchestrator exposes

0 commit comments

Comments
 (0)