{"success":true,"name":"decide-what-delete-means-before-building-it-orphans-cascades","description":"Adding a delete feature for records that other records point to","category":"ai-brimind","path":"ai-brimind/decide-what-delete-means-before-building-it-orphans-cascades/SKILL.md","file":"SKILL.md","content":"---\nname: decide-what-delete-means-before-building-it-orphans-cascades\ndescription: \"Adding a delete feature for records that other records point to\"\n---\n\n# Decide What 'Delete' Means Before Building It: Orphans, Cascades, and Soft Deletes\n\n## When to use\nYou are about to implement deletion for an entity (user, club, product, category) that other data references (orders, members, comments, files), and no decision exists about what happens to those related records — or whether 'delete' should even physically erase anything.\n\n## Example request\nA club is deleted but its members and payments still point to its ID → every screen touching them crashes on 'club not found'; those records are called orphans, and they are born the moment a parent dies without a plan. Deleting a user erases them AND cascades into deleting their orders → the accounting history just vanished; financial and historical records usually must OUTLIVE the entities they reference. A 'deleted' product is only flagged with deletedAt instead of erased → history stays intact and undelete is trivial; this named pattern is soft delete, and its price is that EVERY normal query must now filter the flag. Records with legal weight (invoices, payments) → often may not be hard-deleted at all; 'delete' becomes 'archive/anonymize', a business decision, not a DELETE statement.\n\n## Instructions\nAct as an architect who knows that DELETE is the only operation with no undo button — creating and updating are reversible conversations, deletion is a decision — and that the hard question is never the record itself, it is everything pointing AT it. Before implementing any delete feature, do this: (1) Map the dependents first: list every collection/table that references this entity's ID (members of the club, orders of the user, comments of the post, files of the record). If you cannot list them, search the schemas for the reference — deleting blind is how orphans are made. (2) For each dependent, choose its fate explicitly and say why: CASCADE (children die with the parent — right for records meaningless alone, like a post's comments), RESTRICT (refuse to delete while children exist — 'cannot delete a club that still has members' is often the honest UX), or DETACH/PRESERVE (children survive, keeping the historical reference or a snapshot of the parent's name — right for orders and payments, which are history, not decoration). MongoDB will not enforce this for you the way SQL foreign keys can — in a Mongoose/Next.js stack, these rules live in YOUR delete logic, so they only exist if you write them. (3) Question whether physical deletion is right at all: if users might regret it, if history or audits matter, use soft delete — a deletedAt flag — and name its full cost out loud: every list, search, and count in the app must now exclude flagged records (a default query filter or scope, in ONE shared place, not remembered per query), and unique fields like email need a policy for 'deleted but still occupying the address'. (4) Multi-step deletions (record + its file uploads + its children) are all-or-nothing territory — apply your transaction discipline so a crash mid-delete cannot leave half a corpse. (5) Make the user-facing side match the truth: confirmation that names the consequences ('this will remove 34 members'), and for destructive hard deletes, consider a grace period. Only implement once you can recite: what happens to every dependent, whether the bytes actually disappear, and how the app behaves when it later meets a reference to something deleted — if any answer is 'I guess we'll see', the feature is not designed yet, only dangerous.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #216\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}