Technical document

Designing for Consistency between GCS Files and Soft Deletion

When combining attached files stored in GCS with soft deletion in a DB, we organize how to design the recovery period, deletion queue, and orphan file detection.
Storage / GCS~10 minPublication Date September 22, 2026Update date September 22, 2026
Designing for Consistency between GCS Files and Soft Deletion

Summary

Key points of this document

  • Where are the design challenges?
  • How to define boundaries
  • Details that matter in implementation

Overview

Even if DB records can be soft-deleted, files on GCS have a different lifecycle. If a recovery period is set, the state of the DB and files must be considered together. If only one of GCS files or DB soft deletion is considered correct, inconsistencies will inevitably arise. A design is needed to detect and resolve these discrepancies.

Where are the design challenges?

If data with attached files is soft-deleted and the files are deleted immediately, they cannot be recovered. If they remain, unnecessary files will accumulate, and if permissions are insufficient, deleted data could be accessed.

Files cannot be included in DB transactions. There's a possibility that either the DB update or the GCS operation succeeds while the other fails. A mechanism to restore consistency based on this premise is necessary.

Files deleted in the application might remain in GCS, or files deleted from GCS might still exist in the DB. As long as network failures, manual operations, or batch interruptions occur, complete simultaneous updates of the DB and storage cannot be assumed.

How to define boundaries

During soft deletion, the DB state is set to deleted, and a physical deletion scheduled time is set. The application does not serve files in a deleted state. A job physically deletes items past their scheduled time and records the result.

In the design, a user's delete operation is finalized as a soft delete in the DB, and physical deletion is entrusted to a job after the retention period. GCS objects are given a purpose via prefixes or metadata, allowing them to be correlated with DB records.

Details that matter in implementation

File metadata should include the GCS path, owning record, status, scheduled deletion time, and actual deletion time. For orphan file detection, GCS listings are compared with DB metadata, and files not in the DB are flagged as candidates.

PostgreSQL has a files table with deletedat, purgeafter, storagekey, storagegeneration. Cloud Run Jobs retrieve purge targets and update purged_at after GCS deletion. Storing the GCS generation prevents accidental deletion if a different file is recreated with the same key.

  • Deletion operations should be soft deletions with a recovery period, not immediate GCS deletions.
  • Store GCS object generation to prevent old deletion jobs from deleting new files.
  • Prepare an inventory job to detect states where files exist in DB but not GCS, or in GCS but not DB.

Observing failure modes

Verification includes checking prevention of viewing deleted data, recovery of file references upon restoration, impossibility of restoration after physical deletion, and retries for GCS deletion failures. We also consider listing costs for large numbers of files.

Verification includes checking post-deletion recovery, retention period expiry, GCS deletion failure, DB update failure, and reuse of the same key. Inventory job results are reported to visualize inconsistencies requiring manual intervention.

Discarded options and tradeoffs

While deletion using only GCS lifecycle rules is an option, an application-side deletion queue is effective for retaining business state and reasons. For cost reduction alone, GCS rules suffice; if business auditing is required, DB management should be combined.

A longer recovery period is safer but increases storage costs and information retention risks. A shorter period makes it harder to recover from accidental deletions. It's practical to separate retention policies by feature and file type.

Decision criteria to leave in place

Deletion of GCS files and DB records may not succeed simultaneously. By separating soft deletion, delivery suspension, physical deletion, and orphan detection, both recoverability and storage costs can be balanced.

Consistency between GCS and DB cannot be maintained by a single transaction. By separating soft deletion, physical deletion, and inventory, robust operations can be achieved, assuming discrepancies.

Technical documents

We will continue to increase technical documentation.

We will continue to organize our thoughts on AI, cloud, business app development, requirements definition, and operational design into documents.

Go to technical documentation list