Cascading Delete
Delete a user and automatically delete all their posts, comments, and files. Set up foreign keys with onDelete: 'CASCADE'.
Config
typescript
// posts table
{ name: 'author_id', type: 'relation', sqlType: 'text', notNull: true,
foreignKey: { table: 'users', column: 'id', onDelete: 'CASCADE' } },
// comments table
{ name: 'post_id', type: 'relation', sqlType: 'text', notNull: true,
foreignKey: { table: 'posts', column: 'id', onDelete: 'CASCADE' } },
{ name: 'author_id', type: 'relation', sqlType: 'text', notNull: true,
foreignKey: { table: 'users', column: 'id', onDelete: 'CASCADE' } },
// files table (with R2 cleanup)
{ name: 'owner_id', type: 'relation', sqlType: 'text', notNull: true,
foreignKey: { table: 'users', column: 'id', onDelete: 'CASCADE' } },Set autoDeleteR2Files: true (default) on tables with type: 'file' fields. When a record is deleted -- whether directly or via CASCADE -- its files are removed from object storage.
onDelete options
| Value | Behavior |
|---|---|
CASCADE | Delete child records when parent is deleted |
SET NULL | Set the foreign key to NULL (field must not be notNull) |
RESTRICT | Block the delete if child records exist |
NO ACTION | Same as RESTRICT in SQLite |
SET DEFAULT | Set to the field's default value |