---
title: "Cleanup Test Data"
description: "Wipe test data across all platform databases and external services"
source: /docs/cleanup-test-data
---


Script to reset a development or staging environment by deleting all test data across the platform's databases and external services.

**Location:** `scripts/db/cleanup-test-data.ts`

Usage [#usage]

```bash
# Full cleanup (all databases + external services)
npx tsx scripts/db/cleanup-test-data.ts

# Preview what would be deleted (no changes made)
npx tsx scripts/db/cleanup-test-data.ts --dry-run

# Selective cleanup (only the specified target)
npx tsx scripts/db/cleanup-test-data.ts --auth-db-only
npx tsx scripts/db/cleanup-test-data.ts --db-only          # API database
npx tsx scripts/db/cleanup-test-data.ts --metering-db-only
npx tsx scripts/db/cleanup-test-data.ts --mastra-db-only
npx tsx scripts/db/cleanup-test-data.ts --stripe-only
npx tsx scripts/db/cleanup-test-data.ts --workos-only
npx tsx scripts/db/cleanup-test-data.ts --polar-only
```

Always run `--dry-run` first to verify what will be deleted.

What Gets Cleaned [#what-gets-cleaned]

Auth DB [#auth-db]

Uses `AUTH_DATABASE_URL` + `AUTH_DATABASE_TOKEN`.

Deletes all rows from: `two_factor`, `session`, `account`, `member`, `invitation`, `verification`, `organization`, `user`.

**Preserved:** `jwks` (JWT signing keys — deleting these would invalidate all existing tokens).

API DB [#api-db]

Uses `API_DATABASE_URL` + `API_DATABASE_TOKEN`.

Deletes all rows from: `chat_suggestions`, `chat_votes`, `chat_documents`, `audit_log`, `api_keys`, `usage_records`, `usage_settings`, `invitations`, `trial_history`, `slug_history`, `subscriptions`, `users`, `organizations`.

Tables are deleted in FK-safe order (children before parents).

Metering DB [#metering-db]

Uses `METERING_DATABASE_URL` + `METERING_DATABASE_TOKEN`.

Deletes all rows from: `postpaid_usage`, `contract_overrides`, `threshold_alerts`, `credit_ledger`, `credit_grants`, `metering_events`, `contracts`, `magic_links`.

**Preserved:** `rate_cards`, `rate_card_rates`, `pricing_tiers`, `admin_users`, `admin_api_keys` (configuration data).

Mastra DB [#mastra-db]

Uses `MASTRA_DATABASE_URL` + `MASTRA_DATABASE_TOKEN`.

Deletes all rows from all `mastra_*` tables:

| Category           | Tables                                                                                                                                                                                                      |
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Conversations      | `mastra_messages`, `mastra_threads`                                                                                                                                                                         |
| Observability      | `mastra_ai_spans`, `mastra_traces`                                                                                                                                                                          |
| Memory & resources | `mastra_resources`, `mastra_observational_memory`                                                                                                                                                           |
| Workflows          | `mastra_workflow_snapshot`                                                                                                                                                                                  |
| Agents & skills    | `mastra_agents`, `mastra_agent_versions`, `mastra_skills`, `mastra_skill_versions`, `mastra_skill_blobs`                                                                                                    |
| Datasets & eval    | `mastra_datasets`, `mastra_dataset_items`, `mastra_dataset_versions`, `mastra_experiments`, `mastra_experiment_results`, `mastra_scorers`, `mastra_scorer_definitions`, `mastra_scorer_definition_versions` |
| Prompts            | `mastra_prompt_blocks`, `mastra_prompt_block_versions`                                                                                                                                                      |
| MCP                | `mastra_mcp_clients`, `mastra_mcp_client_versions`, `mastra_mcp_servers`, `mastra_mcp_server_versions`                                                                                                      |
| Workspaces         | `mastra_workspaces`, `mastra_workspace_versions`                                                                                                                                                            |

Also dynamically discovers and cleans **vector tables** (LibSQLVector creates tables with `F32_BLOB` columns).

Stripe [#stripe]

Uses the Stripe REST API. Cancels all active subscriptions, then deletes all customers. Requires `STRIPE_SECRET_KEY`.

WorkOS (optional) [#workos-optional]

Deletes all organization memberships, organizations, and users via the WorkOS REST API. Only runs if `WORKOS_API_KEY` is set.

Polar (optional) [#polar-optional]

Revokes all active subscriptions, then deletes all customers via the Polar REST API. Only runs if `POLAR_ACCESS_TOKEN` and `POLAR_ORGANIZATION_ID` are set. Supports sandbox mode via `POLAR_SANDBOX=true`.

Environment Variables [#environment-variables]

The script loads env vars from multiple sources in this priority order:

1. `process.env` (already set)
2. `.conductor-db-urls` (Conductor workspace overrides)
3. `scripts/env/.env.dev-secrets`
4. Per-app `.dev.vars` / `.env` files
5. Container env files (`container/env/product-1/*.env`)

| Variable                  | Required For                  |
| ------------------------- | ----------------------------- |
| `AUTH_DATABASE_URL`       | Auth DB cleanup               |
| `AUTH_DATABASE_TOKEN`     | Auth DB cleanup               |
| `API_DATABASE_URL`        | API DB cleanup                |
| `API_DATABASE_TOKEN`      | API DB cleanup                |
| `METERING_DATABASE_URL`   | Metering DB cleanup           |
| `METERING_DATABASE_TOKEN` | Metering DB cleanup           |
| `MASTRA_DATABASE_URL`     | Mastra DB cleanup             |
| `MASTRA_DATABASE_TOKEN`   | Mastra DB cleanup             |
| `STRIPE_SECRET_KEY`       | Stripe cleanup                |
| `WORKOS_API_KEY`          | WorkOS cleanup (optional)     |
| `POLAR_ACCESS_TOKEN`      | Polar cleanup (optional)      |
| `POLAR_ORGANIZATION_ID`   | Polar cleanup (optional)      |
| `POLAR_SANDBOX`           | Polar sandbox mode (optional) |

Missing env vars cause that section to be skipped with a warning — the script never fails on missing config.

Safety [#safety]

* **Dry run first** — always use `--dry-run` before running destructively.
* **No production safeguards** — the script does not check which environment it targets. Double-check your env vars point to the correct database before running.
* **Tables that don't exist are silently skipped** — safe to run against a fresh or partially-migrated database.
* **All databases are Turso (LibSQL)** — uses `@libsql/client`. External services (Stripe, WorkOS, Polar) use their REST APIs.
