google-cloud-platformgoogle-cloud-vertex-aikfp

How do I undeploy a model from an endpoint without knowing its id in Vertex AI?


I have managed to undeploy a model from an endpoint using UndeployModelRequest:

    model_name = f'projects/{project}/locations/{location}/models/{model_id}'
    model_request = aiplatform_v1.types.GetModelRequest(name=model_name)
    model_info = client_model.get_model(request=model_request)
    deployed_models_info = model_info.deployed_models
    deployed_model_id=model_info.deployed_models[0].deployed_model_id       
    
    undeploy_request = aiplatform_v1.types.UndeployModelRequest
                       (endpoint=end_point, deployed_model_id=deployed_model_id)

    client.undeploy_model(request=undeploy_request)

but all this depends on knowing model_id. I want to be able to just undeploy a model from an endpoint without knowing the model's id (there will only be one model per endpoint ever). Is that possible or can I get the model id from the endpoint somehow?


Solution

  • You can undeploy all the models from an endpoint by calling the method undeploy_all() (see cleaning up portion).Here you don’t need to pass the model id. This method will be more useful for you since you will be having only one model in an endpoint.

    Below is an example of undeploying all the models from a specific endpoint:

    from google.cloud import aiplatform
    
    def undeploy_model_from_endpoint(names):
        endpoints = aiplatform.Endpoint.list()
        for i in endpoints:
            if str(i.display_name)==names:
                i.undeploy_all()
    
    undeploy_model_from_endpoint("Endpoint_name")
    

    If you wish to undeploy all the models from all the endpoints simply remove the if condition

    endpoints = aiplatform.Endpoint.list()
    for i in endpoints:
            i.undeploy_all()