如何使用Python列出及删除Azure机器学习服务的模型与部署服务
Got it, let's walk through exactly how to list and delete models and deployment services in Azure Machine Learning using Python, including bulk operations. I'll use the modern azure-ai-ml SDK (v2) here since it's the current recommended tooling.
First, make sure you've got the necessary setup:
- Install the SDK:
pip install azure-ai-ml - Authenticate to your Azure ML workspace. The easiest way is using
DefaultAzureCredential, which handles multiple auth methods (service principal, managed identity, interactive login, etc.).
from azure.ai.ml import MLClient from azure.identity import DefaultAzureCredential # Initialize MLClient with your workspace details ml_client = MLClient( credential=DefaultAzureCredential(), subscription_id="YOUR_SUBSCRIPTION_ID", resource_group_name="YOUR_RESOURCE_GROUP", workspace_name="YOUR_WORKSPACE_NAME" )
To fetch every registered model in your workspace, use ml_client.models.list(). This returns a lazy iterator, so you can iterate through it directly or convert it to a list if you need to work with all items at once. You can also filter by model name, tags, or properties if you don't need every model.
# List all models in the workspace all_models = ml_client.models.list() # Print key details for each model for model in all_models: print(f"Model: {model.name} (v{model.version}) | Created: {model.creation_context.created_at}") # Optional: Filter to get all versions of a specific model specific_model_versions = ml_client.models.list(name="my-target-model") for version in specific_model_versions: print(f"Version {version.version} of {version.name}")
Deployments in Azure ML are tied to endpoints (either online or batch). To list all deployments, you'll first fetch all endpoints of each type, then iterate through their associated deployments.
Listing Online Deployments
# Get all online endpoints online_endpoints = ml_client.online_endpoints.list() # For each endpoint, list its deployments for endpoint in online_endpoints: print(f"\nOnline Endpoint: {endpoint.name}") deployments = ml_client.online_deployments.list(endpoint_name=endpoint.name) for deployment in deployments: print(f" Deployment: {deployment.name} | Uses Model: {deployment.model.name} (v{deployment.model.version})")
Listing Batch Deployments
# Get all batch endpoints batch_endpoints = ml_client.batch_endpoints.list() # For each endpoint, list its deployments for endpoint in batch_endpoints: print(f"\nBatch Endpoint: {endpoint.name}") deployments = ml_client.batch_deployments.list(endpoint_name=endpoint.name) for deployment in deployments: print(f" Deployment: {deployment.name} | Uses Model: {deployment.model.name} (v{deployment.model.version})")
Delete a Specific Model
Models in Azure ML are versioned, so you need both the model name and version to delete it:
# Delete a single model version ml_client.models.delete( name="my-model-name", version="1" ) print("Model version deleted successfully.")
Delete a Specific Deployment
To delete a deployment, you need the parent endpoint name and the deployment name:
# Delete an online deployment ml_client.online_deployments.delete( endpoint_name="my-online-endpoint", name="my-deployment-name" ) # Delete a batch deployment ml_client.batch_deployments.delete( endpoint_name="my-batch-endpoint", name="my-deployment-name" )
Note: If you want to delete the entire endpoint (and all its deployments in one go), use
ml_client.online_endpoints.delete(name="my-endpoint")or the equivalent for batch endpoints.
Bulk Listing
Absolutely supported! As you saw in the examples above, the SDK's list() methods return iterable objects that let you fetch all resources of a type (all models, all endpoints, all deployments under endpoints). You can iterate through these lists to process every resource in bulk without extra work.
Bulk Deletion
Azure ML doesn't have a single "one-click" bulk delete API, but you can easily implement bulk deletion by combining listing logic with deletion calls. Here are two common approaches:
1. Sequential Bulk Delete (Simple for Small/Medium Lists)
Great if you don't need maximum speed:
# Bulk delete all versions of a specific model model_name = "my-old-model" models_to_delete = ml_client.models.list(name=model_name) for model in models_to_delete: ml_client.models.delete(name=model.name, version=model.version) print(f"Deleted model {model.name} v{model.version}") # Bulk delete all deployments under an online endpoint endpoint_name = "deprecated-endpoint" deployments_to_delete = ml_client.online_deployments.list(endpoint_name=endpoint_name) for deployment in deployments_to_delete: ml_client.online_deployments.delete(endpoint_name=endpoint_name, name=deployment.name) print(f"Deleted deployment {deployment.name} from endpoint {endpoint_name}")
2. Concurrent Bulk Delete (Faster for Large Lists)
If you have dozens/hundreds of resources to delete, using concurrent calls can speed things up. Just be mindful of Azure's API rate limits—you might want to add delays or use a semaphore to avoid throttling:
import threading def delete_model_safe(model): try: ml_client.models.delete(name=model.name, version=model.version) print(f"Successfully deleted {model.name} v{model.version}") except Exception as e: print(f"Failed to delete {model.name} v{model.version}: {str(e)}") # Get all models to delete all_models = list(ml_client.models.list()) # Spin up threads for parallel deletion threads = [] for model in all_models: thread = threading.Thread(target=delete_model_safe, args=(model,)) threads.append(thread) thread.start() # Wait for all threads to finish for thread in threads: thread.join() print("Bulk model deletion completed.")
- Always double-check which resources you're deleting—once removed, models and deployments can't be recovered.
- If you delete a model version that's being used by a deployment, the deployment will fail. Make sure to delete dependent deployments first, or update them to use a different model version.
- The
azure-ai-mlSDK (v2) replaces the olderazureml-coreSDK—stick to the latest version for the best feature support and bug fixes.
内容的提问来源于stack exchange,提问作者Akshay Godase

