[PYTHON] Clean up the Cloud pak for Data deployment space

Use this when you have created a large number of models, functions, and deployments in the deployment space during model development with Cloud pak for Data (CP4D), and then want to erase them completely.

Such scenes may not occur frequently, but as of June 2020, CP4D v3.0 has restrictions that version control such as models and deployments is not possible, so creating multiple generation models will be quick. The number of models and deployments will increase steadily. I can delete one by one on the CP4D screen, but it's annoying, so I made a python code to erase it all at once.

Launch and run a suitable Notebook in your analysis project.

Preparation # 1. Initialize WMLclient


url = "https://cloudpackfordata.url.com"
from watson_machine_learning_client import WatsonMachineLearningAPIClient
import os
token = os.environ['USER_ACCESS_TOKEN']
wml_credentials = {
    "token" : token,
    "instance_id" : "openshift",
    "url": url,
    "version": "3.0.0"
}
client = WatsonMachineLearningAPIClient(wml_credentials)

Preparation # 2. Set deployment space ID


#ID is client.repository.list_spaces()Can be examined at
space_id = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client.set.default_space(space_id)

The order of erasing all is from the dependency, deployment → model → function.

Delete all deployments


all_deps = client.deployments.get_details()['resources']
for dep in all_deps:
    dep_name = dep['metadata']['name']
    dep_id = dep['metadata']['guid']
    client.deployments.delete(dep_id)
    print(dep_name +"(" + dep_id + ") is deleted")

Delete all models


all_models = client.repository.get_model_details()['resources'] 
for model in all_models:
    model_name = model['metadata']['name']
    model_id = model['metadata']['guid']
    client.repository.delete(model_id)
    print(model_name +" (" + model_id + ") is deleted")

Remove all functions


all_funcs = client.repository.get_function_details()['resources'] 
for func in all_funcs:
    func_name = func['metadata']['name']
    func_id = func['metadata']['guid']
    client.repository.delete(func_id)
    print(func_name +" (" + func_id + ") is deleted")

After deleting, check the clean deployment space on the CP4D screen and finish.

By the way, deleting and recreating the deployment space itself will also instantly clean it up. In that case, the deployment space ID will change, so don't forget to modify various codes.

Also, it is safe to delete this Notebook after it goes live. It is dangerous to leave it.

Recommended Posts

Clean up the Cloud pak for Data deployment space
Deploy functions with Cloud Pak for Data
Start data science on the cloud
Shift the data for 3 months Shift the data for n months
Output log file with Job (Notebook) of Cloud Pak for Data
Eliminate garbled Japanese characters in matplotlib graphs in Cloud Pak for Data Notebook
Cloud Pak for Data object operation example in Python (WML client, project_lib)
Execute API of Cloud Pak for Data analysis project Job with environment variables
I searched for railway senryu from the data
Save pandas data in Excel format to data assets with Cloud Pak for Data (Watson Studio)