Start with the boundary
Use soft deletion without creating hidden data debt is easiest to get right when the boundary is named before the implementation begins. Decide which system owns the decision, which inputs are trusted, what the caller can observe, and what must remain private. That framing prevents a local optimization from quietly becoming an undocumented protocol.
Soft deletion is useful when users need undo, audits need history, or asynchronous cleanup is safer than an immediate delete. It is dangerous when `deleted_at` becomes an informal filter that every query must remember. A deleted row can still appear in a unique constraint, search index, cache, export, or background job. Define the lifecycle and enforce it at multiple layers.
Model the system before choosing a tool
Model deletion state explicitly with deleted_at, deleted_by, reason, and a retention deadline where appropriate. Decide whether deleted records remain visible to owners, administrators, or no one. Use partial uniqueness or an archive table when active and deleted names should coexist. Keep restore semantics separate from undelete-by-update when related records and permissions also change.
Write the model down as a small state diagram or table before selecting a library. Identify the durable state, the derived state, and the transitions that may be retried. This makes it easier to compare a managed service with an in-process implementation and to explain why a particular trade-off is acceptable for this workload.
Design for failure, misuse, and change
A missing filter leaks deleted records, a foreign key prevents cleanup, a search index keeps a stale document, or a job processes a deleted object after the user believes it is gone. Reusing an identifier can also cause an old event to target a new row. Do not promise deletion while backups and replicas retain data outside the documented policy.
A resilient design assumes that inputs are incomplete, dependencies are slow, operators make mistakes, and requirements will change. Put limits at the boundary, return errors that a caller can act on, and preserve enough context to distinguish a bad request from an unavailable dependency. Avoid broad fallbacks that make an unsafe state look successful.
Implementation example
Wrap deletion in a service that records the actor and transitions related resources intentionally. Add repository defaults or database views for active records, but keep explicit administrative queries for the full lifecycle. Emit a tombstone or deletion event for downstream systems and make cleanup idempotent. Enforce a retention horizon with a bounded job rather than an unbounded table scan.
Keep the first implementation narrow enough to review line by line. Make inputs, outputs, authorization context, and failure behavior explicit instead of hiding them behind a convenience helper. The example should be safe to run with synthetic data, emit a correlation identifier, and leave a durable artifact that another engineer can inspect after the request has finished.
create unique index active_projects_slug_uq on projects (tenant_id, slug) where deleted_at is null;Verify and troubleshoot
Test active reads, owner views, admin views, restore, repeated delete, related records, search results, caches, exports, and queued jobs. Verify a deleted record cannot be acted on through a stale URL. Check uniqueness before and after restore, and confirm hard deletion respects legal holds or retention exceptions.
Use a small test matrix that covers the ordinary path, an empty or missing input, a duplicate request, a timeout, a permission failure, and a version mismatch. Assert both the response and the side effects. When a test fails, compare the observed transition with the model rather than adding a retry or widening a timeout without evidence.
Operations and recovery
Monitor tombstone age, cleanup backlog, restore volume, failed downstream deletes, and storage growth. Keep a runbook for accidental deletion, legal hold, and compromised account recovery. Document when data leaves hot storage, when backups expire, and which systems require a deletion acknowledgment.
Give the operator a bounded recovery action: replay a safe event, rebuild a derived view, rotate a credential, drain a queue, or roll back a compatible revision. Record the owner, retention period, alert threshold, and rollback condition next to the implementation. A runbook is useful only when it can be followed without reconstructing the design from production logs.
A practical decision guide
For a small service, prefer the design with the fewest hidden states that still meets the data architecture requirement. Add a managed dependency when it removes a failure mode you can measure, not simply because it is popular. Keep the interface replaceable by isolating provider-specific code behind a narrow adapter and by testing the behavior your users depend on.
Revisit the decision when traffic shape, data sensitivity, team ownership, or recovery objectives change. A design that is excellent for a single tenant or a low-volume internal tool can be the wrong design for a public multi-tenant path. Record the assumptions so the next change starts with evidence rather than folklore.
An implementation checklist
Before publishing a change related to use soft deletion without creating hidden data debt, write down the input contract, authorization context, state transitions, limits, and user-visible errors. Identify the smallest synthetic dataset that demonstrates the normal path and the smallest dataset that demonstrates the dangerous path. Add a correlation ID to the example, make retries deliberate, and decide which artifacts can be retained for support without copying secrets or unnecessary personal data. This checklist is deliberately boring: repeatable release evidence is more valuable than a clever demo.
Use a disposable environment to exercise the implementation with realistic concurrency and a dependency failure. Compare the observed result with the contract, then record the measured latency, resource use, and recovery action. If a managed service or library is involved, pin its version and capture the relevant configuration. Ship behind a reversible change when the behavior is new, and schedule a follow-up review after real traffic reveals assumptions that a test fixture could not.
References and further reading
Use your database constraint documentation, retention and deletion guidance from the relevant privacy regime, and the search or storage provider's lifecycle APIs. Treat the deletion contract as a cross-system data flow, not a flag on one table.
Prefer primary protocol specifications, vendor security documentation, and measured behavior from a disposable environment. Read the failure and deprecation sections, not only the happy-path quick start. A short reference list attached to the code gives future maintainers a way to distinguish an intentional constraint from an accidental implementation detail.