pythonmlflow

How Do You "Permanently" Delete An Experiment In Mlflow?


Permanent deletion of an experiment isn't documented anywhere. I'm using Mlflow w/ backend postgres db

Here's what I've run:

client = MlflowClient(tracking_uri=server)
client.delete_experiment(1)

This deletes the the experiment, but when I run a new experiment with the same name as the one I just deleted, it will return this error:

mlflow.exceptions.MlflowException: Cannot set a deleted experiment 'cross-sell' as the active experiment. You can restore the experiment, or permanently delete the  experiment to create a new one.

I cannot find anywhere in the documentation that shows how to permanently delete everything.


Solution

  • Unfortunately it seems there is no way to do this via the UI or CLI at the moment :-/

    The way to do it depends on the type of backend file store that you are using.

    Filestore:

    If you are using the filesystem as a storage mechanism (the default) then it is easy. The 'deleted' experiments are moved to a .trash folder. You just need to clear that out:

    rm -rf mlruns/.trash/*
    

    As of the current version of the documentation (1.7.2), they remark:

    It is recommended to use a cron job or an alternate workflow mechanism to clear .trash folder.

    SQL Database:

    This is more tricky, as there are dependencies that need to be deleted. I am using MySQL, and these commands work for me:

    USE mlflow_db;  # the name of your database
    DELETE FROM experiment_tags WHERE experiment_id=ANY(
        SELECT experiment_id FROM experiments where lifecycle_stage="deleted"
    );
    DELETE FROM latest_metrics WHERE run_uuid=ANY(
        SELECT run_uuid FROM runs WHERE experiment_id=ANY(
            SELECT experiment_id FROM experiments where lifecycle_stage="deleted"
        )
    );
    DELETE FROM metrics WHERE run_uuid=ANY(
        SELECT run_uuid FROM runs WHERE experiment_id=ANY(
            SELECT experiment_id FROM experiments where lifecycle_stage="deleted"
        )
    );
    DELETE FROM tags WHERE run_uuid=ANY(
        SELECT run_uuid FROM runs WHERE experiment_id=ANY(
            SELECT experiment_id FROM experiments where lifecycle_stage="deleted"
        )
    );
    DELETE FROM runs WHERE experiment_id=ANY(
        SELECT experiment_id FROM experiments where lifecycle_stage="deleted"
    );
    DELETE FROM experiments where lifecycle_stage="deleted";