Skip to main content

Overview

Delete operations in ChameleonDB remove records from your database with built-in safety guards. Like updates, all deletes require a WHERE clause to prevent accidental data loss.
Safety First: ChameleonDB requires a Filter() clause for all deletes. Attempting to delete without a filter will result in a SafetyError.This prevents catastrophic mistakes like:

Basic Delete

Delete a single record by ID:

Delete with Multiple Filters

Use complex criteria to target specific records:

Delete Result

The DeleteResult type shows how many records were removed:

Repository Pattern

Recommended pattern for encapsulating delete operations:

Foreign Key Handling

When deleting records with relationships, be aware of foreign key constraints:

Cascade Deletes

If your schema defines cascade deletes, related records are removed automatically:

Restrict Deletes

If relationships use on_delete restrict, you must delete children first:

Error Handling

Debug Mode

Inspect generated SQL with .Debug():

Full-Table Delete (Use with Extreme Caution)

If you genuinely need to delete all records in a table, use ForceDeleteAll():
EXTREMELY DANGEROUS: ForceDeleteAll() bypasses the safety guard and deletes every record in the table. This is irreversible.Only use this when:
  • You’re in a development/test environment
  • You have complete backups
  • You absolutely understand the consequences
  • You’ve tested the operation thoroughly
NEVER use ForceDeleteAll() in production without:
  • Multiple levels of approval
  • Database backups verified within the last hour
  • A tested rollback plan

Best Practices

Always Verify Before Delete

Use Soft Deletes for Important Data

Check Affected Count

Handle Cascading Deletes Carefully

Common Patterns

Bulk Delete by Criteria

Delete Old Records

Conditional Delete with Verification

HTTP Handler Example

Use Transactions for Complex Deletes

Next Steps

Safety Guards

Learn about ChameleonDB’s mutation safety features

Insert Operations

Create new records with validation

Update Operations

Safely modify existing records

Error Handling

Complete error handling reference