> ## 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.
> Install the library with `cargo add magpiedb`, `npm install magpiedb` or `pip install magpiedb`. Get the server from the GitHub Releases download or the Docker image `ghcr.io/intojhanurag/magpie-server`, as the quick start shows.
> Only one process may write to a store folder at a time. For many apps, send traces to one `magpie-server`.
> `magpie-server doctor` checks a running server. An AI agent can query the traces over MCP at `/mcp` on the HTTP port (page guides/mcp).

# Delete and maintain

> Delete the data of a user, take deleted bytes off the disk, drop old spans, and check the size of a store from Node.js, Python or Rust.

```js Node.js theme={null}
const report = await store.deleteUser('user-7')             // every trace with a span of this user
await store.deleteTraces({ filter: { environment: 'staging' } })
const purge = await store.purgeDeleted()                   // the bytes leave the disk
console.log(purge.segmentsWithDeleted)                     // 0: no file holds deleted data
```

```python Python theme={null}
report = store.delete_user("user-7")              # every trace with a span of this user
store.delete_traces({"filter": {"environment": "staging"}})
purge = store.purge_deleted()                     # the bytes leave the disk
print(purge["segmentsWithDeleted"])               # 0: no file holds deleted data
```

```rust Rust theme={null}
use magpiedb::{DeleteTraces, TraceFilter};

let report = store.delete_user("user-7")?; // every trace with a span of this user
store.delete_traces(&DeleteTraces::Filter(TraceFilter { environment: Some("staging".into()), ..Default::default() }))?;
let purge = store.purge_deleted()?; // the bytes leave the disk
println!("{}", purge.segments_with_deleted); // 0: no file holds deleted data
```

A delete removes whole traces: every span, the summary and the scores. The examples use an open `store` ([open a store](https://docs.magpiedb.com/libraries/overview.md#open-a-store)). The same tasks with the server: [data lifecycle](https://docs.magpiedb.com/operations/data-lifecycle.md).

## Delete traces for privacy

| `deleteTraces(what)`: `what` | Rust | Deletes |
| - | - | - |
| `{ userId }` | `DeleteTraces::UserId` | Every trace that has a span of this user. A trace with spans of two users goes whole. |
| `{ sessionId }` | `DeleteTraces::SessionId` | Every trace that has a span of this session, and the scores on the session. |
| `{ traceIds: [...] }` | `DeleteTraces::TraceIds` | These traces. Their scores go also when a trace has no span. |
| `{ filter }` | `DeleteTraces::Filter` | Every trace that `findTraces` gives for this trace filter. A filter with no condition is an error. |

`deleteUser(userId)` and `deleteSession(sessionId)` are short for the first two. Each returns `{ tracesDeleted, spansDeleted, scoresDeleted, segmentsTouched, segmentsWithDeleted }`.

* The call commits what was written before it, then the delete. It returns when the delete is durable. From then on, no list, count, sum, session, `getTrace`, `getScores` or text search gives the data.
* A crash leaves the store before the delete or after it. It does not leave it between.
* A span written after the call stays, also of the same user. To keep a user out, refuse their spans before they reach the store.
* A filter with a text over the word limit of `contains` and `prefix` is an error: nothing is deleted.
* Copies outside the store folder (backups, S3) are not touched.

## Take deleted data off the disk

A deleted span is hidden at once, but its text stays in its file until that file is written again. `segmentsWithDeleted` counts those files.

`purgeDeleted()` writes those files again now, and waits for it. It commits first, so the write log holds nothing from before the call. With `segmentsWithDeleted: 0` in its answer, no file of the store holds data that was deleted before the call.

The cost is the live spans of those files (`docsRewritten`). For a user with traces at all times, that is the whole store. So delete all users of one request first, then purge once. The answer: `{ segmentsRewritten, docsRewritten, docsRemoved, segmentsWithDeleted, bytesBefore, bytesAfter }`.

## Delete one trace

`deleteTrace(traceId)` removes the spans, the scores and the summary of one trace, at the next commit. It returns nothing. The scores on its session stay. To delete for privacy, with an answer and the bytes off the disk, use `deleteTraces` and `purgeDeleted`.

## Drop old spans

```js Node.js theme={null}
const drop = await store.dropBefore(Date.now() - 30 * 86400000) // { cutoff, spansDropped, tracesDropped, tracesCut, segmentsRemoved }
// or let the store do it: Store.open('./traces', { retentionMs: 30 * 86400000 })
```

```python Python theme={null}
import time

drop = store.drop_before(int(time.time() * 1000) - 30 * 86_400_000)
# or let the store do it: Store.open("./traces", retention_ms=30 * 86_400_000)
```

```rust Rust theme={null}
use std::time::{SystemTime, UNIX_EPOCH};

let now = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis() as i64;
let drop = store.drop_before(now - 30 * 86_400_000)?;
// or let the store do it: Options { retention: Some(Duration::from_secs(30 * 86_400)), ..Default::default() }
```

`dropBefore(cutoffMs)` removes every span that started before the cutoff.

* The cutoff is rounded down to a whole hour, so the sums per hour stay exact. The answer's `cutoff` is the time used.
* A trace with no span left goes with its summary and its scores (`tracesDropped`). A trace with spans on both sides keeps the later ones, and its summary is made again from them, so its start, root and sums can change (`tracesCut`). Session rows and sums are exact after it.
* The call commits what was written before it, then the drop. It returns when the drop is durable.
* Old data goes as whole files (`segmentsRemoved`). The one file that crosses the cutoff keeps its old spans on disk, not in any answer, until all of it is old. If that is more than 7 days away, the file is written again when a third of it is old.
* With the option `retentionMs`, the store drops by itself when it opens and then every 10 minutes. A span is gone at most 1 hour and 10 minutes after it got too old, plus the time of the drop. A store that is not open drops nothing.

## Check the size of a store

| Node.js | Python and Rust | Gives |
| - | - | - |
| `spanCount()` | `span_count()` | Spans in the store. |
| `traceCount()` | `trace_count()` | Traces in the store. |
| `segmentCount()` | `segment_count()` | Segments. |
| `diskBytes()` | `disk_bytes()` | Bytes of all files in the store folder. |
| `maskCounts()` | `mask_counts()` | Matches per mask rule since the store opened. |
| not available | Rust only: `health()` | Can the store take writes: `Ok`, `LowSpace { free, min }`, `Failed(reason)`, `ReadOnly`. |
| not available | Rust only: `refused_writes()` | Writes refused for lack of disk space since the store opened. |
| not available | Rust only: `segments()` | Each segment of both indexes: its size and the time range of its documents. |

> [card] [Errors and input rules](https://docs.magpiedb.com/libraries/errors.md)
>
> A full disk, a closed store, bad input.

> [card] [Data lifecycle](https://docs.magpiedb.com/operations/data-lifecycle.md)
>
> Retention and deletes with the server.


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