Delete the data.
Initialize SDK
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
});
const db = app.rdb();
// Or specify the instance and database
// const db = app.rdb({
// instance: "<instance>",
// database: "<database>"
// });
Delete the data
Use the delete() method to delete data in the table, and combine it with a filter to locate the rows to be deleted.
db.from(tableName).delete(options).filter()
- tableName: Table name
- options: Delete option configuration
Parameter description
| Parameter | Type | Required | Description |
|---|---|---|---|
| options | object | No | Delete option configuration |
options parameter description
| Parameter | Type | Required | Description |
|---|---|---|---|
| count | string | No | Counting algorithm, available values: "exact" - underlying execution of COUNT(*) |
Sample Code
Delete a Single Record
Delete the record with id 1 from the articles table
const { error } = await db.from("articles").delete().eq("id", 1);
console.log('Deletion result:', error ? 'failure' : 'success');
Delete Records in Batches
Delete multiple records with id 1, 2, 3 from the articles table
const { error } = await db.from("articles").delete().in("id", [1, 2, 3]);
console.log('Batch deletion result:', error ? 'failure' : 'success');
Delete with Conditions
Delete all culture and entertainment in draft state
const { error } = await db.from("articles").delete().eq("status", "draft");
console.log('Conditional deletion result:', error ? 'failure' : 'success');
Returned result
{
data: null,
error: null
}
⚠️ Note: The delete() method must be used in conjunction with filters to locate the rows you want to delete. Ensure the conditions you provide accurately indicate all records you plan to delete to avoid accidental data deletion.
Documentation
-Filters - Learn how to use filter conditions