Clean Up Stripe Test Purchases

Remove one Stripe test-mode purchase from Shipflash analytics and customer projections without deleting the Supabase Auth account or touching Lemon Squeezy history.

On this page
  1. 1

    Finish webhook testing first

    Issue the test refund and wait for its webhook and email result before local deletion. A later Stripe event can recreate the local projection.

  2. 2

    Preview the exact target

    Confirm the Checkout Session ID resolves to exactly one Stripe test order, then record its customer, payment intent, charge, refund, dispute, and outbox identifiers.

  3. 3

    Delete dependent projections in one transaction

    Capture the linked IDs first, then remove outbox, activity, dispute, credit, entitlement, refund, charge, and order rows in dependency order.

  4. 4

    Refresh and verify

    Reload revenue analytics and billing history, confirm the order is absent, then run another checkout with the existing Supabase Auth account.

Cleanup ownership

DataCleanup ruleKeep when
billing_ordersDelete the exact Stripe order only after asserting livemode is false.Always keep live orders and unrelated provider orders.
notification_outbox and billing_activity_eventsDelete entries for the Checkout Session, charge, refund, and dispute IDs.Keep provider webhook audit rows unless replaying the exact event intentionally.
billing_disputes, billing_refunds, billing_charges, credits, entitlementsDelete only rows linked to the captured test order.Keep unrelated subscriptions, invoices, usage, and ledger entries.
billing_customersDelete only when no remaining Stripe record references that exact provider customer.Keep by default; never delete by Gmail address alone.
profiles and auth.usersDo not delete for a billing smoke-test reset.Keep the same account so its UUID and non-test history remain stable.
Preview one Stripe test order
select
  provider_order_id,
  provider_customer_id,
  provider_payment_intent_id,
  provider_charge_id,
  profile_id,
  status,
  livemode,
  purchased_at,
  refunded_at
from public.billing_orders
where provider = 'stripe'
  and provider_order_id = 'REPLACE_WITH_CS_TEST_ID'
  and livemode = false;
Transactional local cleanup
begin;

create temporary table cleanup_stripe_order on commit drop as
select provider_order_id, provider_customer_id, profile_id,
       provider_payment_intent_id, provider_charge_id
from public.billing_orders
where provider = 'stripe'
  and provider_order_id = 'REPLACE_WITH_CS_TEST_ID'
  and livemode = false
for update;

do $$
begin
  if (select count(*) from cleanup_stripe_order) <> 1 then
    raise exception 'Expected exactly one Stripe test order';
  end if;
end $$;

create temporary table cleanup_stripe_charge on commit drop as
select provider_charge_id
from public.billing_charges
where provider = 'stripe'
  and livemode = false
  and (
    provider_order_id in (select provider_order_id from cleanup_stripe_order)
    or provider_charge_id in (select provider_charge_id from cleanup_stripe_order)
  );

create temporary table cleanup_stripe_refund on commit drop as
select provider_refund_id
from public.billing_refunds
where provider = 'stripe'
  and livemode = false
  and (
    provider_order_id in (select provider_order_id from cleanup_stripe_order)
    or provider_charge_id in (select provider_charge_id from cleanup_stripe_charge)
  );

create temporary table cleanup_stripe_dispute on commit drop as
select provider_dispute_id
from public.billing_disputes
where provider = 'stripe'
  and livemode = false
  and provider_charge_id in (select provider_charge_id from cleanup_stripe_charge);

delete from public.notification_outbox
where resource_id in (select provider_order_id from cleanup_stripe_order)
   or resource_id in (select provider_charge_id from cleanup_stripe_charge)
   or resource_id in (select provider_refund_id from cleanup_stripe_refund)
   or resource_id in (select provider_dispute_id from cleanup_stripe_dispute);

delete from public.billing_activity_events
where provider = 'stripe'
  and (
    (resource_type = 'checkout_session' and resource_id in (select provider_order_id from cleanup_stripe_order))
    or (resource_type = 'charge' and resource_id in (select provider_charge_id from cleanup_stripe_charge))
    or (resource_type = 'refund' and resource_id in (select provider_refund_id from cleanup_stripe_refund))
    or (resource_type = 'dispute' and resource_id in (select provider_dispute_id from cleanup_stripe_dispute))
  );

delete from public.billing_disputes
where provider = 'stripe'
  and provider_dispute_id in (select provider_dispute_id from cleanup_stripe_dispute);

delete from public.billing_credit_transactions
where provider = 'stripe'
  and provider_order_id in (select provider_order_id from cleanup_stripe_order);

delete from public.billing_credit_grants
where provider = 'stripe'
  and provider_order_id in (select provider_order_id from cleanup_stripe_order);

delete from public.billing_entitlements
where provider = 'stripe'
  and source_type = 'one_time'
  and source_id in (select provider_order_id from cleanup_stripe_order);

delete from public.billing_refunds
where provider = 'stripe'
  and provider_refund_id in (select provider_refund_id from cleanup_stripe_refund);

delete from public.billing_charges
where provider = 'stripe'
  and provider_charge_id in (select provider_charge_id from cleanup_stripe_charge);

delete from public.billing_orders billing_order
using cleanup_stripe_order target
where billing_order.provider = 'stripe'
  and billing_order.provider_order_id = target.provider_order_id
  and billing_order.livemode = false;

commit;
Delete an unreferenced Stripe test customer
delete from public.billing_customers customer
where customer.provider = 'stripe'
  and customer.provider_customer_id = 'REPLACE_WITH_TEST_CUSTOMER_ID'
  and not exists (select 1 from public.billing_subscriptions row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
  and not exists (select 1 from public.billing_subscription_items row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
  and not exists (select 1 from public.billing_orders row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
  and not exists (select 1 from public.billing_invoices row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
  and not exists (select 1 from public.billing_charges row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
  and not exists (select 1 from public.billing_refunds row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
  and not exists (select 1 from public.billing_disputes row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
  and not exists (select 1 from public.billing_usage_events row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
  and not exists (select 1 from public.billing_credit_grants row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
  and not exists (select 1 from public.billing_credit_transactions row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
  and not exists (select 1 from public.billing_entitlements row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
  and not exists (select 1 from public.billing_activity_events row where row.provider = customer.provider and row.provider_customer_id = customer.provider_customer_id)
returning customer.provider_customer_id;