Skip to main content

Delete Data

Initialize the SDK

import cloudbase from "@cloudbase/js-sdk";

const app = cloudbase.init({
env: env: "your-env-id", // Replace 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.

To delete data in the table using the delete() method, you need to use a filter to locate the rows to be deleted.

db.from(tableName).delete(options).filter();
  • tableName: table name
  • options: delete options configuration

Parameter Description

ParameterTypeRequiredDescription
optionsobjectNodelete options configuration

options Parameter Description

ParameterTypeRequiredDescription
countstringNoCounting algorithm, optional values: "exact" - underlying execution of COUNT(*)

Code Example

Delete a single record

// Delete the record with id 1 in the articles table
const { error } = await db.from("articles").delete().eq("id", 1);

console.log("Delete result:", error ? "failed" : "succeeded");

Batch Delete Records

// Batch delete multiple records with ids 1, 2, 3 in the articles table
const { error } = await db.from("articles").delete().in("id", [1, 2, 3]);

console.log("Batch delete result:", error ? "failed" : "succeeded");

Conditional Delete

// Delete all articles where status is 'draft'
const { error } = await db.from("articles").delete().eq("status", "draft");

console.log("Conditional delete result:", error ? "failed" : "succeeded");

Response

{
data: null,
error: null
}

⚠️ Note: The delete() method must be used in conjunction with filters to locate the rows you wish to delete. Ensure the conditions you provide accurately represent all records you intend to delete to avoid accidental data loss.

  • Filter - Learn how to use filter conditions