DELETE

Remove rows from your tables using SQL DELETE statements.

Overview

DELETE FROM removes rows from a table. Always include a WHERE clause to target specific rows — a DELETE without a WHERE clause removes every row in the table.

Basic Syntax

DELETE FROM table_name WHERE condition;

Examples

Delete a Single Row by ID

DELETE FROM users WHERE id = 42;

Delete Multiple Rows by Condition

DELETE FROM sessions WHERE expires_at < NOW();

Delete All Rows Matching a Value

DELETE FROM notifications WHERE user_id = 5 AND is_read = 1;

Delete with LIMIT

MariaDB supports LIMIT on DELETE to cap how many rows can be removed in one statement:

DELETE FROM logs WHERE created_at < '2024-01-01' ORDER BY created_at ASC LIMIT 1000;

This is useful for batched cleanup of large tables.

Safety Note

A DELETE without a WHERE clause removes every row from the table. This cannot be undone.

-- This deletes ALL rows in the table:
DELETE FROM users;

-- This deletes only the intended row:
DELETE FROM users WHERE id = 42;

If you need to remove all rows from a large table efficiently, use TRUNCATE TABLE instead — it is faster and resets the AUTO_INCREMENT counter.

Using DELETE via the API

The CRUD DELETE endpoint handles row removal by ID or by field value:

// Delete by ID
const result = await datasquirel.crud.delete({
    dbName: "my_database",
    tableName: "sessions",
    targetID: 99,
    apiKey: process.env.DATASQUIREL_API_KEY,
});

// Delete by field value
const result = await datasquirel.crud.delete({
    dbName: "my_database",
    tableName: "notifications",
    deleteSpec: {
        deleteKeyValues: [
            { key: "user_id", value: 5, operator: "=" },
            { key: "is_read", value: 1, operator: "=" },
        ],
    },
    apiKey: process.env.DATASQUIREL_API_KEY,
});