SIMPLE_SQL_DELETE_BUILDER

Fluent builder for DELETE statements. Use .where() to specify which rows to delete.

View Source

Destructive Operation

DELETE permanently removes rows. Consider using soft deletes instead for data you might need to recover.

Filtering

where (a_condition: READABLE_STRING_8): like Current
Add WHERE clause to limit which rows are deleted.
where (a_condition: READABLE_STRING_8; a_value: detachable ANY): like Current
WHERE with single parameter binding.
Always Use WHERE

Without a WHERE clause, DELETE removes ALL rows in the table! Always specify conditions.

Execution

execute
Execute the DELETE statement.
to_sql: STRING_8
Generate SQL without executing.

Examples

Delete by ID

db.delete_from ("users")
    .where ("id = ?", user_id)
    .execute

print ("Deleted " + db.changes.out + " rows%N")

Delete with Multiple Conditions

db.delete_from ("sessions")
    .where ("user_id = ? AND expired = 1")
    .execute

Delete Old Records

-- Delete logs older than 30 days
db.delete_from ("logs")
    .where ("created_at < datetime('now', '-30 days')")
    .execute

print ("Cleaned up " + db.changes.out + " old log entries%N")

Delete with Subquery

-- Delete orphaned records
db.delete_from ("order_items")
    .where ("order_id NOT IN (SELECT id FROM orders)")
    .execute

Safe Delete Pattern (Check First)

local
    result: SIMPLE_SQL_RESULT
do
    -- Verify record exists and user has permission
    result := db.select_from ("documents")
        .where ("id = ? AND owner_id = ?")
        .execute

    if not result.is_empty then
        db.delete_from ("documents")
            .where ("id = ?", doc_id)
            .execute
        print ("Document deleted%N")
    else
        print ("Document not found or access denied%N")
    end
end
Soft Delete Alternative

Instead of permanent deletion, consider soft deletes:
db.update("users").set_raw("deleted_at", "datetime('now')").where("id = ?", id).execute