
Summary
Key points of this document
- Where are the design challenges?
- How to define boundaries?
- Details that work in implementation
Overview
In business applications, pressing the delete button doesn't always mean everything is instantly gone. Job design is crucial for progressively handling soft deletes, retention periods, file deletions, and audit logs. The data deletion lifecycle isn't about writing a job to delete, but about making retention periods, holds, deletion proofs, and re-executability operational.
Where are the design challenges?
If images or document files are linked to data deleted by a user, deleting only the DB will leave files in storage. Conversely, if files are deleted first and DB update fails, unreferencable records will remain.
Deletion has multiple requirements, such as a period for recovery from accidental deletion, legal retention periods, audit records, and display to users. If it's completed only by real-time processing, reprocessing upon failure becomes difficult.
The process of deleting old data and expired files tends to be postponed. However, the longer they are stored, the more costs and risks increase, and when it's finally time to delete them, the scope of impact becomes unclear. Deletion is not a batch process, but a lifecycle design.
How to define boundaries?
Upon a deletion request, the status is changed to a soft delete, and the scheduled deletion time is saved. Cloud Run Jobs periodically picks up expired data and sequentially executes GCS file deletion, DB status update, and audit log recording.
In the design, instead of directly deleting targets, they are first extracted as candidates. Retention periods, legal holds, related records, and manual exclusions are checked, and after deletion execution, evidence of what was deleted and when is retained.
Details that work in implementation
The deletion queue should hold target type, target ID, storage path, scheduled time, retry count, and last error. Jobs should limit batch size and record success/failure for each item. GCS deletion should be treated as successful even if the object does not exist.
In Cloud Run Jobs,--dry-run,--limit,--before-datearguments are provided. Delete candidates are locked on PostgreSQL, GCS and DB deletions are executed in stages, and failed targets are left in a state where they can be retried next time.
- For deletion candidates, reason, retentionpolicyversion, and selected_at should be saved.
- Before physical deletion, check references to related data to prevent orphans or broken links.
- Determine the order of GCS deletion and DB update, and prepare compensation processing for failures midway.
Observe how it breaks
In verification, we check for job failures midway, re-execution of the same target, missing GCS files, missing DB records, and exclusion of targets before their retention period. It's also important to be able to trace what was deleted and when from the deletion logs.
In verification, we check if the dry-run count matches the actual execution count, and if re-executing after a midway failure does not result in double deletion. We also check operational logs when there are zero items to delete, when the upper limit is reached, or when there are insufficient permissions.
Discarded options and trade-offs
Strict lifecycle management increases tables and jobs. However, in applications dealing with files, orphan files and missed deletions accumulate, so it's valuable to have a simple deletion queue from the start.
Being too cautious with deletion means data persists, while leaning too much towards immediate deletion makes recovery impossible. It's necessary to design a balance between risk and cost, such as performing physical deletion after a soft delete period.
Decision criteria to leave for the field
Deletion is not a one-off SQL query, but part of the data lifecycle. By separating it into re-executable mechanisms like Cloud Run Jobs, retention periods and physical deletions can be handled safely.
Deletion processing with Cloud Run Jobs is not sufficient if it can only be scheduled. By datafying target selection and evidence, deletion can be operated safely.


