# Admin event and sales cleanup

Platform Admin → **Clear event data** (`admin.html#data-cleanup`). Select an organiser to clear all its events and sales, or select one event. Private events are included. User accounts, organiser profiles, staff assignments, venues, seat maps and reusable organiser configuration remain.

1. Back up the database. Pause sales, imports, webhook processing and ticket/finance delivery workers during the actual cleanup. The orphan-checkout discovery scan can lock shared payment rows, so use a quiet maintenance window even for a single event.
2. Select the target and choose **Preview affected data**. Review the target ID, deletion counts and records whose links will be detached.
3. Enter a required reason/comment (up to 1,000 characters) and the **current password of the signed-in admin**.
4. Choose **Permanently delete selected data**. The preview expires after five minutes and every execution attempt consumes it. Any changed data requires a new preview.

Both mutation endpoints require POST, session CSRF, the platform-admin role and `platform.manage`; existing MFA requirements still apply. Execution attempts are limited per account (five per 15 minutes), independently of IP. Password verification reads the current active account hash and never logs the password. Scope/preview tokens stay in the session/request body. No password or token enters the cleanup audit.

The service deletes scoped orders, ticket/QR/inventory/hold records, payments/refunds/ledger/tax entries, checkout drafts/payments, relevant merchandise/donation/promo records, attendee/offline imports, featured purchases, event-specific settings/role assignments and events. It preserves consent and audit records by detaching deleted order/event links, preserving the existing audit hash chain; scanner devices are retained and unassigned. Event-specific role assignments are deleted rather than broadened to organiser access.

Foreign-key checks remain enabled. Selected row contents are fingerprinted, then re-read under transaction locks before deletion. Unreviewed triggers, nontransactional tables, foreign references/shared records and ownership inconsistencies block cleanup. All database changes and the actor/comment/target/count audit record commit together; failures roll back. The existing organiser event-delete flow still refuses events with sales/configuration and is unchanged.

This clears scoped application records, not external Stripe objects: it does not refund charges or cancel open Stripe Checkout Sessions. Audit metadata, Stripe account ownership/webhook replay protection, uploaded files, shared finance exports and historical records without reliable scope remain. Refresh/clear offline scanner manifests before reusing scanners; locally cached tickets cannot be revoked by a database delete while a device is offline. Global settings can retain historical event slugs; a recreated event should receive a new slug. Shared exports may contain historical data and must be managed separately.

Implementation: `src/Admin/AdminDataCleanupService.php`, `AdminDataCleanupConfirmation.php`, `public/admin-data-cleanup.php`, MVP route wiring, shared API helpers and admin HTML/runtime. No migration, production dependency or deployment-setting changes.

Validation: full canonical foreign-key schema translated into isolated SQLite fixtures with two organisers and three events; organiser/event isolation, user/setup preservation, complete child cleanup, audit-chain validity, rollback, stale content, shared/typed references, orphan checkout discovery, FK/trigger guards. Confirmation/route tests cover password changes, inactive accounts, comments including Unicode, token expiry/replay/scope/actor/session binding, permissions, POST/CSRF and throttling. Eight Node workflow cases cover stale UI responses, secret clearing, required fields, permissions and duplicate submissions. PHP validation used `C:\xampp\php\php.exe` (8.5.13); XAMPP9 PHP was unavailable. MySQL metadata/locking paths received source/security review; they were not executed against a real database. Browser layout verification was stopped by the user before completion. No real cleanup was performed.
