Skip to main content
Version: v0.66.1

Delete registered model version

This page describes how to delete or batch delete registered model versions using the H2O MLOps Python client.

Constants

The following constants are used in the provided sample script:

  • MLOPS_API_URL: The URL endpoint for the MLOps Gateway component.
  • TOKEN_ENDPOINT_URL: The token endpoint URL of the Identity Provider (IDP). A Keycloak example is provided.
  • REFRESH_TOKEN: The user's refresh token for authentication.
  • CLIENT_ID: The client ID for authentication.
  • PROJECT_NAME: The name of the project associated with the registered model.
  • REGISTERED_MODEL_NAME: The name of the registered model.
  • REGISTERED_MODEL_VERSIONS: The registered model version(s) that you want to delete.

Batch deletion constants

  • PROJECT_NAMES: List of project names where the registered model is present.
  • REGISTERED_MODEL_VERSIONS: Dictionary to specify the registered model name and its version that needs to be deleted. The required format is {"model_name": version_number}. Example: {"model": 1}

Functions

  • _get_project_id(projects): Retrieves the ID of a given project based on its name.

  • _get_registered_model_id(models): Fetches the ID of a specified registered model.

  • _get_registered_model_version_ids(models_versions): Ascertain the IDs of the designated registered model versions.

  • _get_project_id(): Input: A list of projects from MLOps. Output: Returns a list of IDs for projects that match the specified names in PROJECT_NAMES.

_get_registered_model_version_ids(): Input: A list of registered model versions from H2O MLOps and a model name. Output: Returns a list of IDs for model versions that match the specified model name and version.

Workflow

  1. Initialize the token provider using the provided refresh token.
  2. Set up the H2O MLOps client with the gateway URL and token provider.
  3. List all projects related to the user.
  4. Get the project ID using the _get_project_id function.
  5. Retrieve a list of registered models within the project.
  6. Get the ID of the registered model with _get_registered_model_id.
  7. Get a list of model versions for the specific registered model.
  8. Determine the ID of the model version to be deleted with _get_registered_model_version_ids.
  9. Delete the specified model version with its ID.

Batch deletion workflow

The following steps only pertain to the batch deletion of registered model versions.

  1. Fetch registered model version IDs for deletion: For each project ID retrieved, the script identifies the registered model version IDs that need to be deleted.

  2. Batch deletion of model versions: Using the model version IDs collected, the batch deletion script sends a batch delete request to H2O MLOps.

Sample script: Delete registered model version

    # List MLOps Projects for the user.
prj: mlops.StorageProject
projects: mlops.StorageListProjectsResponse = mlops_client.storage.project.list_projects(
mlops.StorageListProjectsRequest()
)

try:
prj = _get_project_id(projects.project)
except LookupError:
# Error if a project is not found.
raise ValueError("There is no project with the given name.")

# List Registered models in the project.
models: mlops.StorageListRegisteredModelsResponse = mlops_client.storage.registered_model.list_registered_models(
mlops.StorageListRegisteredModelsRequest(project_id=prj.id)
)

# Get the model id for the registered model.
model: mlops.StorageRegisteredModel
try:
model = _get_registered_model_id(models.registered_models)
except:
raise ValueError("Registered model not found in the project.")

# Get list of versions for the registered model.
model_versions: mlops.StorageListModelVersionsForModelResponse = mlops_client.storage.registered_model_version.list_model_versions_for_model(
mlops.StorageListModelVersionsForModelRequest(registered_model_id=model.id)
)

# Identify the ID for the version to be deleted.
single_model_version_to_be_deleted = _get_registered_model_version_ids(model_versions.model_versions)

# Delete the version.
deleted_registered_model: mlops.StorageDeleteModelVersionResponse = mlops_client.storage.registered_model_version.delete_model_version(
mlops.StorageDeleteModelVersionRequest(id=single_model_version_to_be_deleted)
)

Sample script: Batch delete registered model versions

    # List MLOps Projects for the user.
project_ids: List[str] = []
projects: mlops.StorageListProjectsResponse = mlops_client.storage.project.list_projects(
mlops.StorageListProjectsRequest()
)

try:
project_ids = _get_project_id(projects.project)
except LookupError:
# Error if a project is not found.
raise ValueError("There is no project with the given name.")

model_version_ids_to_be_deleted: List[str] = []
for id in project_ids:
models: mlops.StorageListRegisteredModelsResponse = mlops_client.storage.registered_model.list_registered_models(
mlops.StorageListRegisteredModelsRequest(project_id=id)
)
for model in models.registered_models:
if REGISTERED_MODEL_VERSIONS.get(model.display_name) is None:
continue

models_versions: mlops.StorageListModelVersionsForModelResponse = mlops_client.storage.registered_model_version.list_model_versions_for_model(
mlops.StorageListModelVersionsForModelRequest(registered_model_id=model.id)
)
model_version_ids_to_be_deleted = model_version_ids_to_be_deleted + _get_registered_model_version_ids(
models_versions.model_versions, model.display_name)

deleted_registered_model_versions: mlops.StorageBatchDeleteModelVersionResponse = mlops_client.storage.registered_model_version.batch_delete_model_version(
mlops.StorageBatchDeleteModelVersionRequest(ids=model_version_ids_to_be_deleted)
)

Feedback