Skip to main content

Fixed: mlflow gc Leaves traces.json Files Behind

· 12 min read
Jagdish Kumawat
Founder @ Dewiride

mlflow gc told me an experiment had been permanently deleted, but one of its traces was still on disk, as a traces.json file under mlartifacts. MLflow had saved a spare copy of that trace as a file, and neither deleting the trace nor gc removes it. Deleting the experiment's folder myself, after gc, cleaned it up.

What I Was Doing​

I was cleaning up my MLflow 3.18.0 tracking server after writing a set of tracing how-to posts. The server runs in its own folder, started there with uv run mlflow server --port 8080, so its data lives in that folder: experiments, runs and traces in the database mlflow.db, and files in mlartifacts. Seven experiments from the posts' examples had to go. Their traces came from small Python scripts that used @mlflow.trace, with MLflow's default settings.

Six of the experiments were still active, so I deleted them first with mlflow experiments delete -x <ID>. The seventh, experiment 8, I had deleted earlier. Then I removed all seven for good with mlflow gc, in the server's folder. The server hands out its files itself (artifact proxying), so gc needs MLFLOW_TRACKING_URI set to the server, as its help says:

Terminal (server folder)
export MLFLOW_TRACKING_URI=http://127.0.0.1:8080
uv run mlflow gc --backend-store-uri sqlite:///mlflow.db --experiment-ids 2,3,4,5,6,7,8
  • --backend-store-uri sqlite:///mlflow.db is the server's database, in this folder.
  • --experiment-ids lists the deleted experiments to remove for good, with everything gc deletes along with them.
Output (last lines)
Experiment with ID 4 has been permanently deleted.
Experiment with ID 5 has been permanently deleted.
Experiment with ID 6 has been permanently deleted.
Experiment with ID 7 has been permanently deleted.
Experiment with ID 8 has been permanently deleted.

What I Expected​

The mlflow gc documentation says that when it deletes experiments, it "removes the experiment record and all associated data". I expected nothing left of experiment 8 in mlartifacts/8.

What I Got​

I looked for trace files in mlartifacts:

Terminal (server folder)
find mlartifacts -name traces.json

find lists every file named traces.json inside mlartifacts, at any depth. In PowerShell, Get-ChildItem -Recurse -Filter finds the same files, and Resolve-Path -Relative prints their paths from the current folder; PowerShell 7.6 on my Mac printed the same list, each path starting with ./.

Output
mlartifacts/1/traces/tr-259b95fb327152e8b6bf089997c3fea9/artifacts/traces.json
mlartifacts/1/traces/tr-0b187f6a78766fe305f2165684b9e6b5/artifacts/traces.json
mlartifacts/1/traces/tr-489f48a7df80c7cd613448fc53d665f1/artifacts/traces.json
mlartifacts/1/traces/tr-af60cbc38c91ce09454dd34e2f602681/artifacts/traces.json
mlartifacts/8/traces/tr-4364dea12925bad740c36e6ccb5104bd/artifacts/traces.json

The terminal: find mlartifacts -name traces.json lists four traces.json files under mlartifacts/1/traces and one under mlartifacts/8/traces, for trace tr-4364dea12925bad740c36e6ccb5104bd, although gc had removed experiment 8.

gc had removed experiment 8, yet mlartifacts/8 still held one trace's traces.json. The other four files are in experiment 1, which I kept: it holds the traces of a Claude-powered agent, eight in all.

What I Checked​

Whether MLflow still knows these traces. Each file sits in a folder named after its trace's ID, so a short script can ask MLflow about each one. mlflow.get_trace() loads a trace by its ID, and returns None when there's no such trace. Every trace also carries the tag mlflow.trace.spansLocation, which says where its spans, the steps the trace recorded, are stored. I saved this as check_traces.py in the server's folder:

check_traces.py
"""For each traces.json under mlartifacts: does MLflow still have that trace, and where are its spans?"""

from pathlib import Path

import mlflow

mlflow.set_tracking_uri("http://127.0.0.1:8080")

for file in sorted(Path("mlartifacts").rglob("traces.json")):
trace_id = file.parts[3] # mlartifacts/<experiment ID>/traces/<trace ID>/artifacts/traces.json
trace = mlflow.get_trace(trace_id)
if trace is None:
print(f"{trace_id}: no such trace")
else:
print(f"{trace_id}: spans in {trace.info.tags['mlflow.trace.spansLocation']}")

The check_traces.py script in a macOS-style code window: it finds every traces.json under mlartifacts, takes the trace ID from the fourth part of the path, loads the trace with mlflow.get_trace, and prints either no such trace or the trace's mlflow.trace.spansLocation tag.

  • Path("mlartifacts").rglob("traces.json") finds every traces.json inside mlartifacts, like find did.
  • file.parts[3] is the fourth part of the file's path, the folder named after the trace's ID.
  • trace.info.tags holds the trace's tags, mlflow.trace.spansLocation among them.

With the server running, I ran it in the server's folder; the command is the same in every shell:

Terminal (server folder)
uv run python check_traces.py
Output
/Users/you/source/mlflow-server/.venv/lib/python3.15/site-packages/google/protobuf/json_format.py:1065: FutureWarning: invalid character '+' in URL-safe Base64 data will be discarded in future Python versions
return base64.urlsafe_b64decode(padded_value)
/Users/you/source/mlflow-server/.venv/lib/python3.15/site-packages/google/protobuf/json_format.py:1065: FutureWarning: invalid character '/' in URL-safe Base64 data will be discarded in future Python versions
return base64.urlsafe_b64decode(padded_value)
tr-0b187f6a78766fe305f2165684b9e6b5: spans in TRACKING_STORE
tr-259b95fb327152e8b6bf089997c3fea9: spans in TRACKING_STORE
tr-489f48a7df80c7cd613448fc53d665f1: spans in TRACKING_STORE
tr-af60cbc38c91ce09454dd34e2f602681: spans in TRACKING_STORE
2026/12/03 10:52:01 WARNING mlflow.tracing.fluent: Failed to get trace from the tracking store: RESOURCE_DOES_NOT_EXIST: Trace with ID 'tr-4364dea12925bad740c36e6ccb5104bd' not found.. If using async trace logging, pass flush=True to wait for pending writes. For full traceback, set logging level to debug.
tr-4364dea12925bad740c36e6ccb5104bd: no such trace

The terminal: uv run python check_traces.py prints two FutureWarning lines from protobuf, then four trace IDs with spans in TRACKING_STORE, then a warning that trace tr-4364dea12925bad740c36e6ccb5104bd was not found, and no such trace for it.

The two FutureWarning lines come from protobuf on Python 3.15, while MLflow reads the traces, and have nothing to do with this problem: Fixed: "FutureWarning: invalid character '+' in URL-safe Base64 data will be discarded in future Python versions" When You Read MLflow Traces.

MLflow no longer has trace tr-4364…: its record went with experiment 8, and only the file is left. The four traces of experiment 1 still exist, and their spans are in TRACKING_STORE, the tracking server's database. So their traces.json files are spare copies: the spans they hold are in mlflow.db too.

Why only some traces have a file. Experiment 1 holds eight traces, and only four of them have a traces.json. MLflow issue #27258 explains it. With async trace logging, which is on by default, the client hands a finished trace to the server in two background tasks: one sends its spans to the database, the other records the trace and uploads traces.json, unless the server already says the spans are in the database. When the trace is recorded before the spans are saved, the client doesn't see that yet, and uploads the file as well. The issue adds that reads of these traces use the database, so nothing reads the file. Whether it happens depends on timing: four of the eight agent traces in experiment 1 got a file, and one of the two traces in experiment 8 did, while in a test on a fresh server, 20 traces of a tiny function got none.

What gc and trace deletion remove. In MLflow's code, on 3.18.0 and on the main branch, mlflow gc removes an experiment by deleting its records from the database, without touching its folder in mlartifacts (mlflow/cli/__init__.py). Deleting a trace whose spans are in the database removes only its database rows as well (sqlalchemy_store.py). I added this to the issue, with the output above, in a comment on #27258.

Why It Happens​

Two things meet. Async trace logging sometimes uploads a traces.json for a trace whose spans are already in the database, and neither deleting the trace nor mlflow gc deletes trace files from mlartifacts. So the file outlives its trace, and then its experiment, with nothing in MLflow pointing at it.

The Fix​

Once gc has removed an experiment for good, delete the experiment's folder under mlartifacts yourself. Its name is the ID in gc's line Experiment with ID 8 has been permanently deleted., 8 here. In the server's folder:

Terminal (server folder)
rm -r mlartifacts/8

rm -r deletes the folder and everything in it, and prints nothing when it succeeds. Remove-Item -Recurse does the same in PowerShell.

warning

Only delete the folder of an experiment that gc has removed for good. An experiment that's only deleted can still be restored with mlflow experiments restore, and its folder holds its runs' files.

Leave the traces.json files of traces that still exist, such as the four in experiment 1. They're spare copies, and they go when their experiment is removed and its folder deleted.

After the Fix​

The folder deleted, the same search again:

Output
mlartifacts/1/traces/tr-259b95fb327152e8b6bf089997c3fea9/artifacts/traces.json
mlartifacts/1/traces/tr-0b187f6a78766fe305f2165684b9e6b5/artifacts/traces.json
mlartifacts/1/traces/tr-489f48a7df80c7cd613448fc53d665f1/artifacts/traces.json
mlartifacts/1/traces/tr-af60cbc38c91ce09454dd34e2f602681/artifacts/traces.json

The terminal after the fix: rm -r mlartifacts/8 prints nothing, and find mlartifacts -name traces.json lists only the four files under mlartifacts/1/traces.

Experiment 8's file is gone, and the only traces.json files left belong to traces that still exist.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting mlflow gc --experiment-ids to remove the experiment's filesA trace's traces.json stayed in mlartifacts/8After gc, look in the experiment's folder and delete it yourself
Assuming every trace writes a traces.jsonOnly four of experiment 1's eight traces had oneSearch the whole folder with find after gc

Troubleshooting​

SymptomLikely causeFix
find: mlartifacts: No such file or directoryYou're not in the server's folder, or the server hasn't stored any files yetRun it in the folder where you start mlflow server
RESOURCE_DOES_NOT_EXIST: Trace with ID '...' not found.. from mlflow.get_trace()The trace was deleted, and only its file is leftOnce its experiment is removed with gc, delete the experiment's folder
FutureWarning: invalid character '+' in URL-safe Base64 data will be discarded in future Python versionsprotobuf on Python 3.15, while MLflow reads tracesIt's unrelated to the leftover files

FAQ​

Is it safe to delete the folder?​

For an experiment that gc has removed, yes. MLflow has no record of its traces anymore, as check_traces.py showed with no such trace, so nothing points at the files.

Will an MLflow update fix this?​

On MLflow 3.18.0, #27258 is open. A fix for it stops new spare copies, but files written before it stay until you delete them.

Does deleting a single trace remove its file?​

Not according to MLflow's code. For a trace whose spans are in the database, deleting it removes only its database rows, as the code linked under What I Checked shows. The file stays in the experiment's folder until you remove the experiment and delete its folder.

Conclusion​

mlflow gc removed experiment 8, but a traces.json that async trace logging had saved for one of its traces stayed in mlartifacts/8. MLflow no longer knew that trace, so deleting the experiment's folder myself, after gc, removed the last of it.

Additional Resources​