How do I configure my Azure ML Workspace such that when I delete experiments and jobs, the corresponding Blobs in Azure Blob Storage are also deleted

Chiran Bopitiya 0 Reputation points
2025-03-04T08:03:34.5866667+00:00

I regularly submit jobs and experiments to train ML models. With each experiment, all my files and outputs are uploaded to my blob storage account. I have been constantly deleting failed and cancelled experiments, but I found out that deleting experiments via the Azure ML workspace frontend does not actually delete the files in the Blob Storage and I am still paying for the storage costs of deleted experiments. I have so many experiments, many of which I wish to delete and some important ones that I wish to keep. It is extremely tedious to manually delete all of them one by one via the Azure Storage Explorer. How do I configure my Azure ML workspace such that when I delete an experiment, the files from Blob Storage are also deleted?

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
3,170 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 29,711 Reputation points
    2025-03-04T20:36:29.55+00:00

    You can automate the deletion of blobs associated with deleted experiments using a script.

    You can schedule this script to run periodically using Azure Functions, Azure Automation, or even a simple cron job on a virtual machine.

    from azureml.core import Workspace
    from azure.storage.blob import BlobServiceClient, ContainerClient
    import os
    
    # Load your Azure ML workspace
    ws = Workspace.from_config()
    
    # Get the default datastore (usually the blob storage account)
    default_ds = ws.get_default_datastore()
    
    # Connect to the blob storage account
    blob_service_client = BlobServiceClient(account_url=default_ds.account_url, credential=default_ds.credential)
    container_client = blob_service_client.get_container_client(default_ds.container_name)
    
    # List all experiments
    experiments = ws.experiments
    
    # Iterate through experiments and delete blobs for deleted experiments
    for exp in experiments:
        if exp.archived:  # Check if the experiment is deleted/archived
            print(f"Deleting blobs for experiment: {exp.name}")
            # List blobs in the experiment's folder
            blobs = container_client.list_blobs(name_starts_with=f"experiments/{exp.name}/")
            for blob in blobs:
                print(f"Deleting blob: {blob.name}")
                container_client.delete_blob(blob.name)
    
    print("Blob cleanup completed.")
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.