Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for MySQL Difficulty: Advanced ClaudeChatGPTCursor

MySQL Large-Table Archival & Batch Delete Prompt

Design a chunked archive-and-purge for a huge table that avoids long locks, replication lag, and runaway undo/history growth.

Target user
DBAs and backend engineers purging or archiving high-volume tables in production
Difficulty
Advanced
Tools
Claude, ChatGPT, Cursor

The prompt

You are a senior MySQL/MariaDB DBA who purges and archives multi-hundred-million-row tables without taking the application down. You understand undo/history-list growth, gap and next-key locking on range deletes, replication lag from large transactions, and why chunked deletes driven off the primary key beat OFFSET pagination and unbounded DELETEs.

I will provide:
- The table's `SHOW CREATE TABLE` (PK, indexes, FKs, size): [PASTE]
- Row count, on-disk size, and daily growth: [DESCRIBE]
- The retention rule (e.g. delete/archive rows older than N days by a timestamp column) and whether rows must be copied to an archive table/store first: [DESCRIBE]
- Replication topology and the maximum tolerable replica lag: [DESCRIBE]
- Maintenance window constraints and current disk headroom: [DESCRIBE]

Work through this:

1. **Choose the driving key.** Delete/archive in chunks bounded by the primary key or a covering indexed key range, not by OFFSET and not by the timestamp alone if it is unindexed — explain why range-on-PK keeps each chunk cheap and lock-bounded.
2. **Size the chunk and the pace.** Recommend a starting batch size and an inter-batch sleep tied to replica lag (pause/back off when Seconds_Behind_Source or lag from performance_schema exceeds the threshold). Explain how to keep each transaction short so the history list stays small.
3. **Handle the archive-first case.** If rows must be preserved, describe the INSERT ... SELECT into the archive (or export) plus DELETE as one small transaction per chunk so a crash cannot lose or double-archive rows; note idempotency.
4. **Address locking and FKs.** Call out gap locks under REPEATABLE READ on range deletes, ON DELETE cascade blast radius, and whether READ COMMITTED reduces locking safely here.
5. **Plan space reclamation** separately: when a shared tablespace won't shrink, and how/when to OPTIMIZE TABLE or rebuild off-peak, including the disk headroom needed.
6. **Recommend tooling** where appropriate (pt-archiver with --limit/--sleep/--check-slave-lag) versus a bespoke loop, with the trade-offs.

Output: (a) Chunking strategy and driving key, (b) Batch size + pacing rule tied to replica lag, (c) Archive-first transaction shape (if needed), (d) Locking/FK cautions, (e) Space-reclaim plan, (f) Tooling recommendation and rollback/interruption behavior.

Guardrails: never issue a single unbounded DELETE on a large table; cap each transaction and pace against measured replica lag; confirm ON DELETE CASCADE reach before deleting parents; schedule the space-reclaim rebuild off-peak with verified disk headroom and a tested rollback.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Why this prompt works

The most common way engineers take down a healthy database is a single “DELETE FROM events WHERE created_at < …” that seemed reasonable in a dev shell. This prompt refuses that path outright and instead makes the model design a chunked, primary-key-driven purge — the only approach that keeps each transaction short, locks bounded, and undo/history growth under control. Driving off the PK rather than OFFSET or an unindexed timestamp is the detail that separates a purge that finishes from one that gets slower every batch.

It treats replication as a first-class constraint. A large delete on the source is a large delete on every replica, so the prompt ties batch pacing directly to measured replica lag and demands a back-off rule. That is the difference between a purge that runs quietly overnight and one that pages the whole team because every read replica fell minutes behind.

Finally, the prompt separates deletion from space reclamation, which engineers routinely conflate. Deleting rows from a shared tablespace frees nothing on disk, so the prompt plans the OPTIMIZE/rebuild step explicitly, off-peak, with verified headroom. The archive-first transaction shape and idempotency requirement keep the operation crash-safe, so an interruption never means lost or double-archived data.

Related prompts

More MySQL prompts & error guides

Browse every MySQL prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.