DELETE

DELETE immediately removes the specified document from your keyspace. DELETE uses the following syntax:

DELETE FROM keyspace-ref
[USE KEYS clause]
[WHERE clause]
[LIMIT clause]
[RETURNING clause]

keyspace-ref

Specifies the data source from which to delete the document. You can add an optional namespace name to the keyspace name in this way:

[ (namespace-name :)] keyspace-name

For example, main:customer indicates the customer keyspace in the main namespace. If the namespace name is omitted, the default namespace in the current session is used.

*USE KEYS clause*(optional)

Specifies the keys of the data items to be deleted. Keys can be any expression.

USE [PRIMARY] KEYS expression

*WHERE clause*(optional)

Specifies the condition that needs to be met for data to be deleted.

WHERE condition

*LIMIT clause*(optional)

Specifies the greatest number of objects that can be deleted. This clause must have a non-negative integer as its upper bound.

LIMIT expression

*RETURNING clause*(optional)

Returns the data you deleted as specified in the result_expression.

RETURNING (result-expression [, result-expression]* | [RAW | ELEMENT ] expression)

RBAC Privileges

User executing the DELETE statement must have the Query Delete privilege granted on the target keyspace/bucket. 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.

Table 1.
# Delete Query Contains Query Delete Permissions Needed Query Select Permissions Needed

Q1

WHERE clause

Yes

No

Q2

a subquery

Yes

Yes

Q3

RETURNING clause

Yes

Yes

Q1 Examples that require the Query Delete privilege on `travel-sample`.

DELETE FROM `travel-sample` WHERE type = "hotel";

DELETE FROM `travel-sample` WHERE type = "hotel" RETURNING *;

Q1 Example that requires the Query Delete privilege on `travel-sample` and on `beer-sample`.

DELETE FROM `travel-sample` WHERE city
IN (SELECT raw city FROM `beer-sample` WHERE city IS NOT MISSING);

Q2 Example that requires the Query Delete and Query Select privilege on `travel-sample`.

DELETE FROM `travel-sample`
WHERE city =  (SELECT MAX(city) FROM `travel-sample`

Q3 Example that requires the Query Delete and Query Select privilege on `travel-sample`.

DELETE FROM `travel-sample`
WHERE city = "San Francisco"
RETURNING meta().id

Example 4: Query that deletes product10.

DELETE FROM product p USE KEYS "product10" RETURNING p

"results": [
    {
        "p": {
            "categories": [
                "Luggage"
            ],
            "color": "sky blue",
            "dateAdded": "2014-05-06T15:52:18Z",
            "dateModified": "2014-05-06T15:52:18Z",
            "description": "This product is available on
                \u003ca target=\"_blank\"
                href=\"http://www.amazon.com/gp/product/
                B005HNKFSM/ref=s9_hps_bw_g198_ir011?pf_rd_m=ATVPDKIKX0DER\
                u0026pf_rd_s=merchandised-search-5\u0026pf_
                rd_r=D182EDFE2F434403B401\u0026pf_rd_t=101\
                u0026pf_rd_p=1486061902\u0026pf_rd_i=15743161
                \"\u003eAmazon.com\u003c/a\u003e.",
            "imageURL": "http://ecx.images-amazon.com/
                images/I/51KiHy-Y-2L._SY220_.jpg",
            "name": "Briggs \
                u0026 Riley Luggage Executive Clamshell Backpack",
            "productId": "product10",
            "reviewList": [
                "review47",
                "review873",
                "review1224",
                "review2203",
                "review2242",
                "review6162",
                "review6825",
                "review7300",
                "review9934"
            ],
            "type": "product",
            "unitPrice": 231.2
        }
    }
]

Example 5: Query that deletes the product that is priced at 5.25.

DELETE FROM product p
WHERE p.unitPrice = 5.25
RETURNING p.productId

"results": [
        {
            "productId": "product99"
        }
    ]