# Deleted and hidden rows

https://docs.serverpod.dev/concepts/data-and-the-database/database/offline-sync/deleted-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:

| Reason                                                                          | Visibility          |
| ------------------------------------------------------------------------------- | ------------------- |
| 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](https://docs.serverpod.dev/concepts/data-and-the-database/database/crud.md#delete), such as `deleteRow`. When the row is a parent, the relation's `onDelete` action applies, as in any database:

| `onDelete`                      | When the parent has visible children                                             |
| ------------------------------- | -------------------------------------------------------------------------------- |
| `Restrict`, `NoAction`, or none | The delete throws.                                                               |
| `Cascade`                       | The children are hidden too.                                                     |
| `SetNull` or `SetDefault`       | Each 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](https://docs.serverpod.dev/concepts/data-and-the-database/database/offline-sync/modeling.md#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:

```dart
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`:

```dart
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:

   ```dart
   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:

   ```dart
   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:

```dart
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](https://docs.serverpod.dev/concepts/data-and-the-database/database/offline-sync/spaces.md#read-and-write-on-the-device).

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`](https://docs.serverpod.dev/concepts/data-and-the-database/database/offline-sync/how-merges-work.md#unique-values-after-a-merge). 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](https://docs.serverpod.dev/concepts/data-and-the-database/database/offline-sync/how-merges-work.md#which-edit-wins), 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.

## Related

- [How merges work](https://docs.serverpod.dev/concepts/data-and-the-database/database/offline-sync/how-merges-work.md): why a merge hides a row or undoes a delete.
- [Model synced data](https://docs.serverpod.dev/concepts/data-and-the-database/database/offline-sync/modeling.md): choosing `onDelete` for each relation.
