Skip to main content
del is a method that deletes multiple records at once by ID array. It executes safely within a transaction and requires admin permissions by default.
del is not defined in BaseModelClass. It’s a standard pattern automatically generated by the Syncer in each Model class when you create an Entity.

Type Signature

Auto-Generated Code

Sonamu automatically generates the following code based on your Entity:
How it works:
  1. getPuri(“w”): Gets write Puri from BaseModelClass method
  2. transaction(): Starts transaction
  3. table().whereIn().delete(): Executes delete using Knex methods
  4. Returns ids.length: Returns count of IDs requested for deletion

Parameters

ids

Array of IDs for records to delete. Type: number[]

Return Value

Type: Promise<number> Returns the length of the passed ID array (ids.length). This is not the number of rows actually deleted from the database.
The return value is not the number of rows actually deleted from the database. Non-existent IDs are silently ignored without errors, and the return value is always ids.length.

Basic Usage

Delete Single Record

Delete Multiple Records

Conditional Delete

Transactions

del automatically executes within a transaction.

With @transactional

Permission Check

By default, del requires admin permissions.

Delete Own Data

Practical Examples

API Usage

Auto-Generated del API

Client Code

React (TanStack Query)

Foreign Key Constraints

CASCADE Setting

RESTRICT Setting

Manual Handling

Deletion Verification

Check Existence

Verify After Delete

Performance Optimization

Batch Delete

Split large deletions into batches.

Index Utilization

Add indexes on foreign keys that are frequently deleted.

Cautions

1. Pass as Array

del must receive an array.

2. Return Value is ids.length

3. CASCADE Caution

CASCADE settings can delete unintended data.
Use transactions when deleting from multiple related tables.

5. Permission Check

Be careful when changing default guards: ["admin"] setting.

Soft Delete vs Hard Delete

Hard Delete (del)

Pros:
  • Saves disk space
  • Simple structure
Cons:
  • Cannot recover
  • History lost

Soft Delete (save)

Pros:
  • Recoverable
  • Preserves history
  • Audit trail
Cons:
  • Increases disk usage
  • Query complexity increases

Next Steps

save

Save and update records

findMany

Query records to delete

@transactional

Safe deletion with transactions

Foreign Keys

Configure foreign key constraints