88 lines
2.1 KiB
Plaintext
88 lines
2.1 KiB
Plaintext
---
|
|
title: DELETE | Datasquirel docs
|
|
description: Remove rows from a table using DELETE statements in Datasquirel
|
|
page_title: DELETE
|
|
page_description: 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
|
|
|
|
```sql
|
|
DELETE FROM table_name WHERE condition;
|
|
```
|
|
|
|
## Examples
|
|
|
|
### Delete a Single Row by ID
|
|
|
|
```sql
|
|
DELETE FROM users WHERE id = 42;
|
|
```
|
|
|
|
### Delete Multiple Rows by Condition
|
|
|
|
```sql
|
|
DELETE FROM sessions WHERE expires_at < NOW();
|
|
```
|
|
|
|
### Delete All Rows Matching a Value
|
|
|
|
```sql
|
|
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:
|
|
|
|
```sql
|
|
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.
|
|
|
|
```sql
|
|
-- 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](/docs/api-reference/crud/delete) handles row removal by ID or by field value:
|
|
|
|
```javascript
|
|
// 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,
|
|
});
|
|
```
|