Scrivio Interview library
ScenarioAdvanced6 min read · updated 13 November 2026

A document platform stores 1.2 PB in object storage. An inventory report shows that about 40 percent of objects are not referenced by any database row, mostly from failed uploads, deleted documents and abandoned multipart uploads, and storage cost grows 8 percent a month.

How do you clean this up safely, and how do you change the design so the database and the object store stay consistent?

Clean up from a bucket inventory report joined with a database export, delete only objects unreferenced for longer than a grace period such as 7 days, and delete in throttled batches with versioning as a safety net after the owning teams have checked a sample. Then fix the write order so it cannot recur: create the row as PENDING before the upload and mark it READY after, and on deletion mark the row DELETED and delete the object asynchronously with retries. Lifecycle rules, a continuous reconciliation job with an orphaned-bytes metric, and a visible cost per document keep it that way.

Why this question is asked

Orphaned objects are the quiet cost of almost every system that keeps files in object storage. Every failed upload, every document deleted from the database but not from the bucket, and every abandoned multipart upload leaves bytes that nobody will ever read but everyone pays for. At 1.2 PB with 40 percent orphaned, that is roughly 480 TB of waste, growing every month. Interviewers ask this because the cleanup is risky (deleting a referenced document is far worse than paying for storage) and because the real answer is a design change, not a script. In this answer, we will see where orphans come from, plan a safe cleanup, change the write order so the database and the store stay consistent, add continuous reconciliation, and answer the follow-up about proving to legal that nothing referenced was deleted.

1Where Orphans Come From

The database and the object store are two systems with no shared transaction, so any operation that touches both can stop halfway. If the service uploads the object first and then the insert fails, or the client uploads and never confirms, the object exists with no row. If a document is deleted by removing the row and the object deletion fails, or was never attempted, the bytes remain. Multipart uploads that were started and never completed leave parts that do not even appear in a normal listing but are billed.

The fix has two halves: remove the existing orphans safely, and change the write paths so new ones are not created, or are removed automatically.

2Building the Cleanup List Safely

We do not list 1.2 PB through the API. Instead we turn on a bucket inventory report, such as S3 Inventory, which delivers a daily list of every object with its key, size and last-modified time as Parquet files. We export the referenced keys from the database at the same time, and join the two in a query engine such as Athena, Trino or Spark. An object is a candidate only if no row references it and it is older than a grace period, for example 7 days. The grace period protects in-flight work: an upload that started an hour ago may not have its row marked yet, and a delayed replica export may miss very recent rows.

SQL
-- Candidates: in the bucket, unreferenced, older than 7 days
SELECT i.key, i.size
FROM inventory_2026_11_13 i
LEFT JOIN db_doc_keys d ON d.object_key = i.key
WHERE d.object_key IS NULL
AND i.last_modified_date < current_date - INTERVAL '7' DAY;
-- result: about 480 TB in N objects, grouped by prefix for review

Before deleting anything, we group the candidates by prefix and by owning team and send each team a sample to check. This finds the surprising cases, such as a reporting job that reads objects by key without a database row, or a second database that also references the bucket.

3Deleting in Throttled Batches With a Safety Net

With the list approved, we delete in batches, for example with S3 Batch Operations or a job that calls the multi-object delete API with up to 1,000 keys per request, at a controlled rate so we do not compete with production traffic. Versioning is turned on for the bucket beforehand, with a lifecycle rule that permanently removes noncurrent versions after, say, 14 days. A delete then only adds a delete marker, and for two weeks we can undo any mistake by removing the marker. After the window, the bytes are gone and the bill falls.

We also start small: one prefix, one team, then wider, watching for any error report from the product before moving on.

4Changing the Write Order

The lasting fix is to make the database the record of every object, before and after it exists. On upload, the service creates the row with status PENDING and the chosen key before the client uploads, and marks it READY after the upload is confirmed. So every object that exists has a row, and a PENDING row older than a day is a known failed upload whose object can be removed.

On deletion, the service marks the row DELETED in the user's transaction, so the document disappears from the product immediately. A background worker then deletes the object and, on success, removes or archives the row; on failure it retries with backoff until it succeeds. Because the row stays until the object is gone, a failed deletion is never forgotten. This is effectively an outbox: the intent is recorded in the database, and the side effect happens later, at least once.

Step throughLifecycle that keeps rows and objects consistent
  1. Insert row PENDING with keybefore any bytes exist
  2. Client uploads objectpresigned URL
  3. Upload confirmed?
    YesMark READYNoPENDING older than 1 day -> remove object and row
  4. User deletes documentrow marked DELETED, hidden at once
  5. Worker deletes objectretries with backoff until success
  6. Object gone -> remove rowno orphan possible

6 steps in order. Play them, or step through them one at a time.

5Keeping It Clean: Lifecycle Rules, Reconciliation and Unit Cost

Lifecycle rules handle the predictable cases without code: abort incomplete multipart uploads after 3 days, and expire temporary prefixes such as exports/ or tmp/ after a week. The reconciliation query from the cleanup becomes a scheduled job that runs on every inventory delivery, and it publishes a metric of orphaned bytes and objects by prefix. If the write paths are right, that metric should stay near zero; a rising line points to a new bug before it becomes a petabyte.

Finally, we make the cost visible where decisions are made. A monthly report of storage cost per stored document, and per product area, tells product owners that their feature's storage is growing 8 percent a month, which is a product conversation (retention, versions kept, original sizes) as much as a platform one.

6Follow-up questions

How would you prove to legal that no referenced document was deleted?

We keep the evidence chain. For each deletion batch we store the candidate list, the inventory and database snapshots it was computed from, the approvals, and the delete logs from S3 Batch Operations or CloudTrail data events. Just before deleting each batch, the job re-checks every key against the live database, and that check is logged too. Versioning gives a recovery window during which any claim can be tested and undone. Documents under legal hold should be protected with Object Lock or a retention rule so no job can delete them at all.

Why not delete the object first and then the row?

If the row deletion then fails, the product shows a document whose bytes are gone, which is a visible error. Deleting the row first and the object later can only leave an orphan for a short time, and the retry worker removes it. Between an invisible cost and a visible failure, we choose the invisible cost and then clean it up.

What grace period is right?

Longer than the longest legitimate gap between upload and confirmation, plus the age of the database export. Seven days is generous for most products, and the cost of keeping orphans a week longer is small.

7Conclusion

In this answer, we cleaned up 480 TB of orphans safely and changed the design so they stop appearing. The cleanup came from an inventory report joined with a database export, limited to objects unreferenced for more than 7 days, checked with the owning teams, and deleted in throttled batches with versioning as an undo window. The design change was the write order: row PENDING before upload and READY after, row marked DELETED first and the object deleted asynchronously with retries. Lifecycle rules, continuous reconciliation with an orphaned-bytes metric, and cost per document reported to product owners keep it under control.

What to say in the interviewAbout 80 seconds aloud

Open with

Orphans happen because the database and the object store have no shared transaction, so failed uploads and half-finished deletes leave bytes behind. For the cleanup I would not list 1.2 petabytes through the API.

Then, if they want more

I would turn on the bucket inventory report, export the referenced keys from the database, and join them in Athena or Spark. Only objects that are unreferenced and older than a grace period, say seven days, are candidates, so in-flight uploads are safe.

I would group candidates by prefix and team and have owners check a sample, then delete in throttled batches with versioning on and a short noncurrent-version retention, so we can undo mistakes for two weeks. To stop new orphans, I change the write order.

The row is created as PENDING with its key before the upload and marked READY after, so every object has a row and stale PENDING rows identify failed uploads. On delete, the row is marked DELETED in the transaction and a worker deletes the object asynchronously, retrying until it succeeds, before removing the row.

Add lifecycle rules for incomplete multipart uploads and temp prefixes, run the reconciliation continuously with an orphaned-bytes metric, and report storage cost per document so product owners see the growth.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you clean this up safely, and how do you change the design so the database and...": https://scrivio-interview-library.netlify.app/architecture/storage-and-pipelines/050cd43961/