quant
quant

Reputation: 4482

How to delete a run_id from MLflow

I want to permanently delete a run_id from an experiment in MLflow

I am using the following code:

import mlflow
from mlflow.entities import ViewType
_mlflow_tracking_uri = "the mlflow tracking url"
exp_id_delete = "2"
mlflow.set_tracking_uri(_mlflow_tracking_uri)
           

client = mlflow.tracking.MlflowClient()
run_infos = mlflow.list_run_infos(exp_id_delete, run_view_type=ViewType.DELETED_ONLY)
               
for r in run_infos:
                    
   client.delete_run(r.run_id)

But I get an error INVALID_PARAMETER_VALUE: The run blabla must be in the 'active' state. Current state is deleted.

Any ideas ?

UPDATE

I tried writing some code with the help of the link that @Alex Ott suggested:

import mlflow
from mlflow.entities import ViewType
from mlflow.store.tracking import DEFAULT_LOCAL_FILE_AND_ARTIFACT_PATH
from mlflow.store.artifact.artifact_repository_registry import get_artifact_repository
from mlflow.tracking import _get_store
from mlflow.entities.lifecycle_stage import LifecycleStage
from mlflow.exceptions import MlflowException
_mlflow_tracking_uri = "the mlflow tracking url"
exp_id_delete = "2"
mlflow.set_tracking_uri(_mlflow_tracking_uri)


client = mlflow.tracking.MlflowClient()
run_infos = mlflow.list_run_infos(exp_id_delete, run_view_type=ViewType.DELETED_ONLY)

for r in run_infos:
      

 print("- run_id: {}, lifecycle_stage: {}".format(r.run_id, r.lifecycle_stage))

 run = backend_store.get_run(r.run_id)
 if run.info.lifecycle_stage != LifecycleStage.DELETED:
      raise MlflowException(
          "Run % is not in `deleted` lifecycle stage. Only runs in "
          "`deleted` lifecycle stage can be deleted." % r.run_id
                    )
 artifact_repo = get_artifact_repository(run.info.artifact_uri)
 artifact_repo.delete_artifacts()
 backend_store._hard_delete_run(r.run_id)
 print("Run with ID %s has been permanently deleted." % str(r.run_id))

But now, even though I get as first message:

 Deleted runs: run_id: 24ac591d1af840cfb703131dbe1f92a9, lifecycle_stage: deleted 

then I get the following error

mlflow.exceptions.MlflowException: Run '24ac591d1af840cfb703131dbe1f92a9' not found

Any ideas ?

Upvotes: 3

Views: 2694

Answers (1)

Alex Ott
Alex Ott

Reputation: 87069

There is a mlflow gc command for that - potentially you can call it from your Python script, or at least do the same as in the source code.

Upvotes: 2

Related Questions