Safely remove database records with precise filtering and cascading considerations across all supported databases.
The delete()
method removes records from database tables based on filter conditions. It provides safe deletion with mandatory filtering to prevent accidental data loss and returns the number of records actually deleted.
Delete operations are permanent and irreversible. Always backup important data and test delete queries carefully before running them on production data.
delete(table_name: str, filters: Dict[str, Any]) -> int
Type: str
Name of the table from which to delete records. The table must exist.
Type: Dict[str, Any]
Dictionary mapping column names to values for identifying which records to delete. All conditions are combined with AND logic. Must be non-empty to prevent accidental deletion of all table data.
🚨 Critical: Empty filters are rejected to prevent accidental table truncation. Use explicit conditions to identify the exact records to delete.
Type: int
Number of records that were actually deleted from the table. Returns 0 if no records matched the filter conditions.
Before deletion: 1: alice (alice@example.com) 2: bob (bob@example.com) 3: charlie (charlie@example.com) Deleted 1 user(s) After deletion: 1: alice (alice@example.com) 3: charlie (charlie@example.com)
Delete multiple records based on specific criteria:
Deleted 1 inactive user(s) Remaining users: 1 alice: Active
Use multiple conditions for precise deletion:
All orders: 1: Customer 1, $49.99, cancelled 2: Customer 1, $149.99, completed 3: Customer 2, $25.5, cancelled 4: Customer 2, $89.99, pending 5: Customer 3, $15.0, cancelled Deleted 1 small cancelled order(s) Remaining orders: 4 Order 1: Customer 1, $49.99, cancelled Order 2: Customer 1, $149.99, completed Order 4: Customer 2, $89.99, pending Order 5: Customer 3, $15.0, cancelled
Handle deletions that may affect related records:
Before deletion: Categories: 1: Electronics 2: Books Products: 1: Laptop (category: 1) 2: Phone (category: 1) 3: Novel (category: 2) Deleted 1 out-of-stock electronics Remaining products: 2 1: Laptop (category: 1) 3: Novel (category: 2)
Uses standard SQL DELETE statements with WHERE clauses:
Uses MySQL DELETE syntax with parameter binding:
Uses PostgreSQL DELETE syntax:
Translates to MongoDB deleteMany operation:
Table not found: no such table: nonexistent_table No records match the deletion criteria Error: Filters required for deletion - refusing to delete all records Table 'missing_table' does not exist
Consider implementing soft deletion for critical data:
After deleting records, you might want to: