SurrealDB Docs Logo

Enter a search query

delete()

Deletes all records in a table, or a specific record, from the database.

Method Syntax
db.delete(resource)

Arguments

ArgumentDescription
resource

The table name or a record ID to select. Will also accept a tuple of record name and ID.

Example usage

use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; use surrealdb::opt::auth::Root; use surrealdb::opt::Resource; use surrealdb::RecordId; #[derive(Debug, Serialize, Deserialize)] struct Person { id: RecordId, } #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; db.signin(Root { username: "root", password: "root", }) .await?; db.use_ns("ns").use_db("db").await?; // Create three `person` records db.create(Resource::from(("person", "one"))).await?; db.create(Resource::from(("person", "two"))).await?; db.create(Resource::from(("person", "three"))).await?; let deleted_one: Option<Person> = db.delete(("person", "one")).await?; dbg!(deleted_one); let deleted_rest: Vec<Person> = db.delete("person").await?; dbg!(deleted_rest); Ok(()) }

Translated query

While SurrealQL’s DELETE statement returns an empty array by default, this function translates into a query that adds a RETURN BEFORE clause to return the deleted items.

DELETE FROM $resource RETURN BEFORE;

See also

On this page

© SurrealDB GitHub Discord Community Cloud Features Releases Install