Skip to content

MegaKassa data purge runbook

MegaKassa has been removed from application code. Legacy databases may still contain payment_provider_configs.provider = 'megakassa' rows. Leaving that config row in place breaks listProviderConfigs() after deploy because megakassa is no longer in HOSTED_PAYMENT_PROVIDERS.

This purge is irreversible. Always take a database backup first.

Prerequisites

  1. Deploy (or stage) a build that no longer registers MegaKassa adapters.
  2. Snapshot / dump Postgres (pg_dump or your managed-backup tool).
  3. Confirm restore procedure for that snapshot.
  4. Set DATABASE_URL to the target database.

Dry-run (mandatory)

bash
pnpm repair:megakassa-purge

Review printed counts for:

  • payment_provider_configs
  • payment_provider_methods
  • cashier_payment_methods
  • payment_method_routes / payment_route_providers / usage counters / preferences
  • payments / withdrawals
  • classified balance_transactions and audit_log rows

Confirm Fynzah SBP (fynzah:sbp:in), Fintrix, CryptoCloud, P2P, and Mercado Pago rows are not listed.

Pending work

If the dry-run aborts because MegaKassa payments/withdrawals are still pending / processing / created:

  1. Prefer settling or cancelling them in the admin UI, or
  2. Re-run with an explicit override after operator sign-off:
bash
pnpm repair:megakassa-purge -- --apply --allow-pending

Apply

bash
pnpm repair:megakassa-purge -- --apply

The script runs all deletes in one transaction, then asserts zero remaining MegaKassa provider config/method/payment rows.

Re-run --apply a second time; it must report zero rows (idempotent).

Integrity notes

  • Ledger rows are deleted only when reference_id matches a proven MegaKassa payment id, order id, or withdrawal id.
  • Ambiguous audit_log metadata that mentions MegaKassa outside payment-domain entity types causes an abort — classify manually before retrying.
  • Deleting credited ledger rows does not automatically rewrite wallet balances. If balances must match historical ledger totals, restore from the pre-purge backup instead of inventing corrective ledger entries.

Restore

Restore the pre-purge snapshot with your standard Postgres restore process, then re-run application migrations if required. Do not partially re-insert MegaKassa config into a post-removal codebase.

Verification after purge

bash
# Should find no application references (docs/runbook/script excluded intentionally)
rg -i 'megakassa' --glob '!apps/docs/**' --glob '!scripts/repair-megakassa-purge*' --glob '!node_modules'

pnpm --filter @repo/db exec vitest run src/operations/wallet/providers

Confirm Admin → Cashier methods loads without errors and Fynzah SBP still works.