Skip to main content
Version: Next

Deleted and hidden rows

A delete hides a synced row instead of removing it, so the delete can sync to other devices and be undone. You can read hidden rows, list what users deleted, and restore rows.

What hides a row​

A row is hidden for one of three reasons. Offline sync stores the reason as a CrdtDataRowVisibility value:

ReasonVisibility
A user deleted it, on any device.userDelete
A user deleted its parent, and Cascade deleted it too.userCascadeDelete
A merge hid it, because its parent was deleted on another device or is missing.foreignKeyCascade

Delete rows​

Delete a synced row with the usual delete methods, such as deleteRow. When the row is a parent, the relation's onDelete action applies, as in any database:

onDeleteWhen the parent has visible children
Restrict, NoAction, or noneThe delete throws.
CascadeThe children are hidden too.
SetNull or SetDefaultEach child's foreign key is set to null or its default, as an edit that syncs.

Hidden children never block a delete. When another device adds a child at the same time, Choose onDelete for each relation shows what happens.

warning

Hidden rows are never removed, so deleted data stays on the server and on every device that synced it. Keep data you may have to erase permanently, such as personal details, in a table that doesn't sync.

Read hidden rows​

Hidden rows stay out of queries, includes, and watches. To include them, add t.includeHiddenRows to a where clause:

final tasks = await Task.db.find(
session,
where: (t) => t.projectId.equals(projectId) & t.includeHiddenRows,
);

The result mixes visible and hidden rows. To limit it to one space, add t.spaceEquals(spaceUuid). You can also use t.includeHiddenRows in the where clause of an includeList.

The findById method never returns a hidden row, so read one by id with findFirstRow:

final task = await Task.db.findFirstRow(
session,
where: (t) => t.id.equals(taskId) & t.includeHiddenRows,
);

Build a trash​

To list only what users deleted, without the rows a merge hid, run these steps in your app:

  1. Read the ids of the rows users deleted. The CrdtDataRow table records each row's visibility:

    final deleted = await CrdtDataRow.db.find(
    session,
    where: (t) =>
    t.tbl.name.equals(Task.t.tableName) &
    t.space.uuidSpaceId.equals(spaceUuid) &
    t.visibility.equals(CrdtDataRowVisibility.userDelete),
    );

    That table isn't limited to the user's spaces, so keep the space filter.

  2. Load those rows, including the hidden ones:

    final trash = await Task.db.find(
    session,
    where: (t) =>
    t.id.inSet(deleted.map((row) => row.uuidRowId).toSet()) &
    t.includeHiddenRows,
    );

Don't run these steps in an endpoint, where they could return other users' deleted rows.

Restore a row​

To restore a deleted row, insert it again with the same id, on a device that has the deleted row. The restore syncs to every device:

await Task.db.insertRow(session, deletedTask);

For a row in a shared space, run the insert in transactionForUser with the space's UUID, as for any write to a shared space.

A hidden row gives up its unique values, so another row can use them. Text in its unique fields reads with a suffix, such as Groceries__hidden__<rowId>.

Insert the row with that value unchanged. Offline sync recognizes it and restores the row's right to Groceries. If you change it back to Groceries yourself, the restore throws DatabaseUniqueViolationException when another row holds that value.

A restore follows these rules:

  • A restore writes the whole row again. Like any full-row save, it gives every column a new timestamp. Edits with an older timestamp lose to it, and edits with a newer one still win, even if this device hasn't received them yet.
  • Children deleted with the parent stay deleted. Rows a merge hid come back with their parent. To bring back rows that a Cascade delete hid, insert them again too.
  • Some writes restore rows without meaning to. An insert with ignoreConflicts: true, an upsert, or an upsertRow brings back a row the device holds as deleted.
  • A device that never had the row can't restore it. Its insert counts as new and loses to the delete.