The naive version is one statement: DELETE FROM orders WHERE created_at < '2019-01-01'. On a table this size that is one giant transaction. In MySQL it fills the undo log and holds row locks for the whole run, and the replicas replay it as one huge transaction. In PostgreSQL it writes a WAL record for every row and leaves a billion dead tuples for vacuum. And if anything fails at minute 50, the whole thing rolls back, which can take as long again.
Delete in small batches instead, by primary key, and give the system room to breathe between them:
-- MySQL: one batch; repeat from a script until it removes 0 rows
DELETE FROM orders
WHERE created_at < '2019-01-01'
ORDER BY id
LIMIT 5000;
-- between batches: check replica lag, sleep a little, stop if lag passes your limit
-- PostgreSQL: PostgreSQL has no DELETE ... LIMIT, so pick the batch by key
DELETE FROM orders
WHERE id IN (SELECT id FROM orders WHERE created_at < '2019-01-01' ORDER BY id LIMIT 5000);
Percona Toolkit has pt-archiver, which does the same batching and can copy rows to an archive table before deleting them. In MongoDB, a large deleteMany has the same replication problem, so batch it too. For data that should always expire, use a TTL index and let the server delete in the background.
The rows are gone. Why is the table still 800 GB?Deleting does not give space back to the operating system. In PostgreSQL the space becomes reusable for new rows after vacuum, but the files keep their size. Shrinking them takes VACUUM FULL, which locks the table for the whole rewrite, or pg_repack, which does it online. In MySQL, InnoDB keeps the pages for reuse too. A rebuild (OPTIMIZE TABLE, which is an online rebuild for InnoDB, or gh-ost with a no-op ALTER) returns the space. If you delete old data every month, there is a far better design: partitions. That is the next chapter.