---
title: "Recipe: Cascading Delete"
description: "Delete a user and their posts, comments and files in teenybase with onDelete: 'CASCADE' foreign keys."
url: https://teenybase.com/docs/recipes/cascading-delete
last_updated: 2026-04-19
---

> Documentation index: https://teenybase.com/llms.txt lists every teenybase docs page with a link to its Markdown version. All docs in one file: https://teenybase.com/llms-full.txt

# 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 |
