> ## Documentation Index
> Fetch the complete documentation index at: https://docs.magpiedb.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Magpie is a trace store for AI agents, used as a library (crate `magpiedb`, npm `magpiedb`, PyPI `magpiedb`) or as `magpie-server`, which takes OpenTelemetry (OTLP over HTTP on 127.0.0.1:4318 and gRPC on 127.0.0.1:4317 by default).
> Use only the commands, flags, routes and JSON shapes that these pages show. Do not guess a flag or a query field: if a page does not show it, say that the docs do not show it.
> Packages and server binaries are not published yet: build from source, as the quick start shows. Check the status note on the introduction page before you suggest `cargo add`, `npm install` or `pip install`.
> Only one process may write to a store folder at a time. For many apps, send traces to one `magpie-server`.

# Retention and deletes

> Drop old spans with --retention. Delete a person's data and purge it from the disk.

## Retention

`--retention 30d` keeps spans for 30 days (also `h`, `m`, `s`). The server drops older spans by itself: when a store opens, and then every 10 minutes. With tenants, it opens each tenant's store once an hour to do it.

The sample data of the [query guide](https://docs.magpiedb.com/queries/overview.md) has times in 1970. So with any retention, it all goes when the server opens the store:

```sh theme={null}
jq -c '.spans[], .scores[]' "$REPO/testdata/query-examples.json" > sample.ndjson
magpie-server import --dir ./old --format magpie sample.ndjson > /dev/null 2>&1
magpie-server --dir ./old --retention 30d
```

```sh theme={null}
sleep 2   # the drop runs in the background after the start
curl -s localhost:4318/v1/count -d '{}'; echo
curl -s localhost:4318/v1/traces/find -d '{"limit": 0}'
```

```text theme={null}
{"count":0}
{"count":0,"traces":[],"nextCursor":null}
```

* The drop at the start runs in the background. For a moment after the start (about 1 s with this small store), queries can still find the old spans.
* The cutoff is `now - retention`, rounded down to a whole hour. A span is gone at most 1 hour and 10 minutes after it got too old (about 2 hours and 10 minutes for a tenant with no requests), plus the time of the drop.
* A trace with no span left goes with its summary and its scores. A trace with spans on both sides of the cutoff keeps the newer spans.

> [!WARNING]
>
> Old data goes as whole files. The one file that crosses the cutoff keeps its old spans on disk (not in any answer) until all of it is old. So there is no fixed time by which the bytes of a dropped span leave the disk. For a privacy request, use a delete and a purge (below).

## Deletes for privacy, and purge

A person asks you to delete their data (a GDPR request). Delete every trace of that user, then take the bytes off the disk.

```sh theme={null}
magpie-server --dir ./traces
```

```sh theme={null}
"$REPO/scripts/send-trace.sh" --url http://127.0.0.1:4318 --user user-42 > /dev/null
"$REPO/scripts/send-trace.sh" --url http://127.0.0.1:4318 --user user-7 > /dev/null
curl -s localhost:4318/v1/traces/delete -H 'content-type: application/json' -d '{"userId": "user-42"}'; echo
curl -s -X POST localhost:4318/v1/purge -H 'content-type: application/json' | jq -c '{segmentsWithDeleted}'
curl -s localhost:4318/v1/traces/find -d '{"filter": {"userId": "user-42"}, "limit": 0}'
```

```text theme={null}
{"tracesDeleted":1,"spansDeleted":3,"scoresDeleted":0,"segmentsTouched":...,"segmentsWithDeleted":...}
{"segmentsWithDeleted":0}
{"count":0,"traces":[],"nextCursor":null}
```

* `segmentsTouched` and `segmentsWithDeleted` count files of the store; they depend on the commits before.
* `POST /v1/traces/delete` takes one of `{"userId": ...}`, `{"sessionId": ...}`, `{"traceIds": [...]}` or `{"filter": traceFilter}` (the filters of the [query guide](https://docs.magpiedb.com/queries/overview.md)). It deletes whole traces: every span, the summary and the scores. A trace with spans of two users goes whole.
* The answer comes after the commit: from then on no query returns the data, and a crash leaves the store before the delete or after it.
* The bytes stay in the store's files until those files are written again (`segmentsWithDeleted` counts them). `POST /v1/purge` writes them again now. With `"segmentsWithDeleted": 0`, no file of the store holds data that was deleted before the call. A purge can take minutes on a large store: give the client a long timeout. Delete all the users of one request first, then purge once.
* A span that arrives after the delete is kept, also of the same user.
* Both routes need the `write` right, and `Content-Type: application/json` (`curl -d` alone sends another type: `415`).

> [!WARNING]
>
> Copies outside the store folder (backups, exports, S3) are not touched by a delete or a purge.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.