DELETE
statement
The DELETE
statement can be used to delete records from the database.
Statement syntax
SurrealQL SyntaxDELETE [ ONLY ] @targets
[ WHERE @condition ]
[ RETURN NONE | RETURN BEFORE | RETURN AFTER | RETURN DIFF | RETURN @statement_param, ... ]
[ TIMEOUT @duration ]
[ PARALLEL ]
;
Example usage
Basic usage
The following query shows basic usage of the DELETE statement, which is used to delete records from a table or a graph edge.
Deleting records can be done in multiple ways:
-- Delete all records from a table
DELETE person;
-- Delete a record with a specific numeric id
DELETE person:100;
-- Delete a record with a specific string id
DELETE person:tobie;
-- Delete just a single record
-- Using the ONLY keyword, just an object for the record in question will be returned.
-- This, instead of an array with a single object.
DELETE ONLY person:tobie;
Deleting records based on conditions
The delete statement supports conditional matching of records using a WHERE
clause. If the expression in the WHERE
clause evaluates to true, then the respective record will be deleted.
-- Update all records which match the condition
DELETE city WHERE name = 'London';
By default, the delete statement does not return any data, returning only an empty array if the statement succeeds completely. Specify a RETURN
clause to change the value which is returned for each document that is deleted.
-- Don't return any result (the default)
DELETE user WHERE age < 18 RETURN NONE;
-- Return the changeset diff
DELETE user WHERE interests CONTAINS 'reading' RETURN DIFF;
-- Return the record before changes were applied
DELETE user WHERE interests CONTAINS 'reading' RETURN BEFORE;
-- Return the record after changes were applied
DELETE user WHERE interests CONTAINS 'reading' RETURN AFTER;
Using TIMEOUT duration records based on conditions
When processing a large result set with many interconnected records, it is possible to use the TIMEOUT
keywords to specify a timeout duration for the statement. If the statement continues beyond this duration, then the transaction will fail, no records will be deleted from the database, and the statement will return an error.
DELETE person WHERE ->knows->person->(knows WHERE influencer = false) TIMEOUT 5s;
Deleting graph edges
You can also delete graph edges between two records in the database by using the DELETE statement.
For example the graph edge below:
RELATE person:tobie->bought->product:iphone;
[
{
"id": "bought:ctwsll49k37a7rmqz9rr",
"in": "person:tobie",
"out": "product:iphone"
}
]
Can be deleted by:
DELETE person:tobie->bought WHERE out=product:iphone;