Skip to main content

Delete Traces

You can delete traces based on specific criteria using the mlflow.client.MlflowClient.delete_traces() method. This method allows you to delete traces by experiment ID, maximum timestamp, or trace IDs.

tip

Deleting a trace is an irreversible process. Ensure that the parameters provided to the delete_traces API meet the intended range for deletion.

Deletion Methods

Delete traces older than a specific timestamp:

import time
from mlflow import MlflowClient

client = MlflowClient()

# Get current timestamp in milliseconds
current_time = int(time.time() * 1000)

# Delete traces older than current time, limit to 100 traces
deleted_count = client.delete_traces(
experiment_id="1", max_timestamp_millis=current_time, max_traces=100
)

print(f"Deleted {deleted_count} traces")

Delete traces older than a specific time period:

from datetime import datetime, timedelta

# Calculate timestamp for 7 days ago
seven_days_ago = datetime.now() - timedelta(days=7)
timestamp_ms = int(seven_days_ago.timestamp() * 1000)

deleted_count = client.delete_traces(
experiment_id="1", max_timestamp_millis=timestamp_ms
)

Advanced Use Cases

Delete traces based on specific criteria:

import mlflow
from mlflow import MlflowClient


def delete_error_traces(experiment_id: str):
"""Delete only traces that resulted in errors"""
client = MlflowClient()

# Search for error traces
traces = mlflow.search_traces(
experiment_ids=[experiment_id],
filter_string="status = 'ERROR'",
max_results=1000,
)

if traces:
trace_ids = [trace.info.trace_id for trace in traces]
deleted_count = client.delete_traces(
experiment_id=experiment_id, trace_ids=trace_ids
)
print(f"Deleted {deleted_count} error traces")
return deleted_count

return 0


# Usage
delete_error_traces("1")

Best Practices

Always test first: Use search queries or dry-run mode to verify which traces will be deleted before proceeding.

Delete in batches: For large numbers of traces, delete in smaller batches to avoid timeouts and performance issues.

Set reasonable limits: Use the max_traces parameter to prevent accidentally deleting too many traces at once.

Monitor and log: Keep track of deletion activities for audit purposes, especially in production environments.

Handle errors gracefully: Implement proper error handling to manage network issues, permission problems, or invalid parameters.

Parameters Reference

ParameterTypeDescription
experiment_idstrRequired. The ID of the experiment containing traces to delete
max_timestamp_millisintDelete traces created before this timestamp (in milliseconds)
trace_idsList[str]Delete traces with these specific trace IDs
max_tracesintMaximum number of traces to delete in this operation
note

You must specify either max_timestamp_millis or trace_ids, but not both. The max_traces parameter cannot be used with trace_ids.