Skip to content
clientdb

Search the docs

Find a page or section

Reference

Archive vs hard delete

Archive hides a row everywhere and keeps it, reversibly, until retention deletes it; hard delete removes the row and the rows that cascade from it, and can still be undone.

In the todo app, finishing a project shouldn't destroy it: people may want it back. Deleting a single todo, on the other hand, should simply remove it. clientdb supports both.

Archive

Archiving hides a row on every device and keeps it in the database. Mark the model archivable; it gets two fields, archivedAt and archivedBy.

With Prisma, add the two columns to the model and set archivable in fromPrisma:

prisma/schema.prisma
model Project {
  id         String    @id @default(uuid())
  name       String
  inviteCode String    @unique
  archivedAt DateTime? @db.Timestamptz(3)
  archivedBy String?
  members    Member[]
  todos      Todo[]
}
src/sync/schema.ts
import { fromPrisma } from "@clientdb/prisma";
import { prismaModels } from "../generated/clientdb";

export const schema = fromPrisma(prismaModels, {
  omit: { User: ["passwordHash"] },
  models: {
    User: { load: "eager" },
    Project: { load: "eager", archivable: true },
    Member: { load: "eager" },
    Todo: { load: "eager" },
  },
});
export type Schema = typeof schema;

A model without both columns is a type error that names them. In a TypeScript schema, pass { archivable: true } to model(), which adds the fields itself.

Who may archive is the archive rule, for archiving and unarchiving alike:

TypeScript
// in projectRules, src/sync/permissions/project.ts
archive: {
  when: (project) => {
    return isAdmin(project);
  },
},

On the device:

src/app/projects/ArchiveButton.tsx
"use client";
import type { Project } from "@/sync/db";

export function ArchiveButton({ project }: { project: Project }) {
  if (project.isArchived) {
    return <button onClick={() => project.unarchive()}>Restore</button>;
  }
  return <button onClick={() => project.archive()}>Archive</button>;
}
  • Default collections hide archived rows: db.project.all leaves them out, db.project.archived.all lists only them, db.project.includeArchived().all both.
  • Archived rows keep syncing, so "Restore" works on every device. A row counts as archived while any row it cascades from (onDelete: Cascade in schema.prisma, onDelete: "cascade" in a TypeScript schema) is, so a project's todos and their comments are hidden with it.
  • archivedBy is the id of the user who archived the row, and archivedAt the server's time; the server sets both from the session and its own clock, whatever the device sent, so retention can't be tricked into deleting a row early. A device that creates or restores a row already archived needs the archive rule as well as create.
  • Archiving is undoable like any change, and Prisma writes to archivedAt sync like any other column.

Deleting archived rows after a while

Archived rows are kept until retention.archived on the engine deletes them:

src/sync/engine.ts
import { createEngine } from "@clientdb/server";
import { permissions } from "./permissions";

export const engine = createEngine({
  permissions,
  retention: { log: "30d", archived: "90d" },
});

engine.prune() (and npx clientdb migrate, after migrating) then deletes rows archived more than 90 days ago, with the rows that cascade from them, and the deletes sync to every device. A row that a restrict relation still points at stays, and is counted in archivedKept. See Retention for running it on a schedule.

Hard delete

TypeScript
const { undoable, unloadedDescendants } = todo.delete();

A hard delete:

  • removes the row;
  • deletes the rows whose relation to it says onDelete: "cascade" (onDelete: Cascade in schema.prisma), and theirs in turn;
  • sets nullable references with onDelete: "setNull" to null;
  • fails with Conflict while a restrict relation points at the row.

On every device that held the row, the delete removes it and its cascading children and leaves a tombstone, so a late update can't bring it back.

Undoing a delete

Undo restores the row and its cascading children with their original ids, and points the references that were set to null back at it. It is only complete if all of them were on this device: comments are lazy, so a todo's comments may not be. delete() reports this as undoable and unloadedDescendants. Pass ensureUndoable to fetch what's missing first, in one request; the row then disappears after that round trip instead of at once:

TypeScript
const { undoable } = await todo.delete({ ensureUndoable: true }); // undoable: true

Which one to use

ArchiveHard delete
Row stays in the databaseyes, until retention.archivedno
Still syncsyes, hidden by defaultno
Cascading childrencount as archiveddeleted
Undoalwayscomplete when the children were on the device
Permission rulearchivedelete