delete()
Deletes all records in a table, or a specific record, from the database.
Method Syntaxdb.delete(resource)
Argument | Description | ||
---|---|---|---|
resource | The table name or a record ID to select. Will also accept a tuple of record name and ID. |
use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; use surrealdb::opt::auth::Root; use surrealdb::opt::Resource; use surrealdb::RecordId; struct Person { id: RecordId, } 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(()) }
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;