DELETE
- reference
DELETE immediately removes the specified document from your keyspace.
Prerequisites
RBAC Privileges
To execute the DELETE statement, you must have the Query Delete privilege granted on the target keyspace. If the statement has any RETURNING clauses that need data read, then the Query Select privilege is also required on the keyspaces referred in the respective clauses. For more details about user roles, see Authorization.
In summary, see the below table.
Delete Query Contains | Query Delete Permissions Needed | Query Select Permissions Needed | Example |
---|---|---|---|
WHERE clause |
Yes |
No |
|
Subquery |
Yes |
Yes |
|
RETURNING clause |
Yes |
Yes |
Syntax
delete ::= 'DELETE' 'FROM' target-keyspace use-keys-clause? where-clause?
limit-clause? returning-clause?
target-keyspace | |
use-keys-clause | |
where-clause | |
limit-clause | |
returning-clause |
Delete Target
target-keyspace ::= keyspace-ref ( 'AS'? alias )?
Specifies the data source from which to delete the document.
keyspace-ref | |
alias |
Keyspace Reference
keyspace-ref ::= keyspace-path | keyspace-partial
keyspace-path ::= ( namespace ':' )? bucket ( '.' scope '.' collection )?
keyspace-partial ::= collection
Keyspace reference for the delete target. For more details, refer to Keyspace Reference.
AS Alias
Assigns another name to the keyspace reference. For details, refer to AS Clause.
Assigning an alias to the keyspace reference is optional.
If you assign an alias to the keyspace reference, the AS
keyword may be omitted.
Delete Hint
You can use a USE KEYS
hint on the delete target to specify the keys of the data items to be deleted.
For details, refer to USE KEYS Clause.
WHERE Clause
where-clause ::= 'WHERE' cond
Specifies the condition that needs to be met for data to be deleted. Optional.
LIMIT Clause
limit-clause ::= 'LIMIT' expr
Specifies the greatest number of objects that can be deleted. This clause must have a non-negative integer as its upper bound. Optional.
RETURNING Clause
returning-clause ::= 'RETURNING' (result-expr (',' result-expr)* |
('RAW' | 'ELEMENT' | 'VALUE') expr)
Specifies the information to be returned by the operation as a query result. For more details, refer to RETURNING Clause.
Examples
Please note that running the following examples will permanently delete your sample data.
To restore your sample data, remove and reinstall the travel-sample bucket.
Refer to Sample Buckets for details.
|
This example requires the Query Delete privilege on `travel-sample`.inventory.hotel
.
DELETE FROM `travel-sample`.inventory.hotel;
This example requires the Query Delete privilege on `travel-sample`.inventory.airport
and the Query Select privilege on `beer-sample`
.
DELETE FROM `travel-sample`.inventory.airport
WHERE city IN (SELECT raw city FROM `beer-sample` WHERE city IS NOT MISSING)
RETURNING airportname;
This example requires the Query Delete and Query Select privileges on `travel-sample`.inventory.airport
.
DELETE FROM `travel-sample`.inventory.airport
WHERE city IN (SELECT RAW MAX(t.city) FROM `travel-sample`.inventory.airport AS t)
RETURNING airportname;
These examples require the Query Delete and Query Select privileges on `travel-sample`.inventory.hotel
.
DELETE FROM `travel-sample`.inventory.hotel RETURNING *;
DELETE FROM `travel-sample`.inventory.hotel
WHERE city = "San Francisco"
RETURNING meta().id;
This example deletes the document airline_4444
.
DELETE FROM `travel-sample`.inventory.airline k
USE KEYS "airline_4444"
RETURNING k
[
{
"k": {
"callsign": "MY-AIR",
"country": "United States",
"iata": "Z1",
"icao": "AQZ",
"name": "80-My Air",
"id": "4444",
"type": "airline"
}
}
]
This example deletes the airline with the callsign "AIR-X".
DELETE FROM `travel-sample`.inventory.airline f
WHERE f.callsign = "AIR-X"
RETURNING f.id
[
{
"id": "4445"
}
]