You are viewing the documentation for a prerelease version.

View Latest

DELETE

    +

    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.

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

    WHERE clause

    Yes

    No

    Example 1

    Subquery

    Yes

    Yes

    Example 2

    RETURNING clause

    Yes

    Yes

    Example 3

    Syntax

    'DELETE' 'FROM' delete-target delete-hint? where-clause? limit-clause? returning-clause?

    Delete Target

    delete-target ::= keyspace-ref [ [ AS ] alias ]
    ( namespace ':' )? keyspace ( 'AS'? alias )?

    Specifies the data source from which to delete the document.

    Keyspace Reference

    keyspace-ref ::= keyspace-path | keyspace-partial
    keyspace-path | keyspace-partial
    keyspace-path ::= [ namespace ':' ] bucket [ '.' scope '.' collection ]
    ( namespace ':' )? bucket ( '.' scope '.' collection )?
    keyspace-partial ::= collection
    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.

    alias

    String to assign an alias.

    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
    where clause

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

    LIMIT Clause

    limit-clause ::= LIMIT expr
    limit clause

    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 )
    '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.
    Example 1. Delete query containing a WHERE clause

    This example requires the Query Delete privilege on `travel-sample`.inventory.hotel.

    DELETE FROM `travel-sample`.inventory.hotel;
    Example 2. Delete queries containing a subquery

    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);

    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);
    Example 3. Delete queries containing a RETURNING clause

    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;
    Example 4. Delete by key

    This example deletes the document 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. Delete by filter

    This example deletes any product that is priced at 5.25.

    DELETE FROM product p
    WHERE p.unitPrice = 5.25
    RETURNING p.productId
    Results
    [
        {
            "productId": "product99"
        }
    ]