Skip to main content

Delete Documents

Delete API is used to delete existing documents that match the filters.

Request Specification

HTTP APIHTTP MethodURL ParamsResource
DELETEDELETE
  • Database Name
  • Collection Name
  • /databases/{db}/collections/{collection}/documents/delete

    Request Payload

    {
    "filter": {
    <field1>: number|string|boolean,
    <field2>: {<$lt|$lte|$gt|$gte>: number},
    <$and|$or>: [<field3>: number|string|boolean, expr1, expr2...]
    }
    }

    Understanding the syntax:

    • The filter syntax is similar as documented in the Query section which allows you to filter and delete only the documents that are matching the criteria.

    Example collection

    The first step is to set up the collection object. All the operations on the collection are performed through this collection object.

    curl 'http://localhost:8081/v1/databases/catalogdb/collections/catalog/createOrUpdate' \
    -X POST \
    -H 'Content-Type: application/json' \
    -d '{
    "schema":{
    "title":"catalog",
    "properties":{
    "id":{
    "type":"integer",
    "format":"int32",
    "autoGenerate":true
    },
    "name":{
    "type":"string"
    },
    "price":{
    "type":"number"
    },
    "brand":{
    "type":"string"
    },
    "labels":{
    "type":"string"
    },
    "popularity":{
    "type":"integer"
    },
    "reviews":{
    "type":"object",
    "properties":{
    "author":{
    "type":"string"
    },
    "rating":{
    "type":"number"
    }
    }
    }
    },
    "primary_key":[
    "id"
    ]
    }
    }'

    Now let's assume an e-commerce website that has the above collection catalog and has 5 products(documents) present in it.

    idnamepricebrandlabelspopularityreviews
    1fiona handbag99.9michael korspurses8{"author": "alice", "rating": 7}
    2tote bag49coachhandbags9{"author": "olivia", "rating": 8.3}
    3sling bag75coachpurses9{"author": "alice", "rating": 9.2}
    4sneakers shoes40adidasshoes10{"author": "olivia", "rating": 9}
    5running shoes89nikeshoes10{"author": "olivia", "rating": 8.5}

    Simple Delete

    A simple delete is by filtering on the primary key of the document. Let's say you need to remove a product from this collection that has an id assigned as 5.

    curl 'http://localhost:8081/v1/databases/catalogdb/collections/catalog/documents/delete' \
    -X DELETE \
    -H "Content-Type: application/json" \
    -d '{
    "filter": {
    "id": 5
    }
    }'

    Range Delete

    Extending the above example, in case the products that you are planning to remove have continuous ids then you can use range delete to remove these products. As an example, if you need to delete documents that have id greater than or equal to 3 but less than 5.

    curl 'http://localhost:8081/v1/databases/catalogdb/collections/catalog/documents/delete' \
    -X DELETE \
    -H "Content-Type: application/json" \
    -d '{
    "filter": {
    "id": {
    "$gte": 3
    },
    "id": {
    "$lt": 5
    }
    }
    }'

    Delete All

    Sometimes, there is a need to delete all the documents then an empty filter can be sent to the server.

    curl 'http://localhost:8081/v1/databases/catalogdb/collections/catalog/documents/delete' \
    -X DELETE \
    -H "Content-Type: application/json" \
    -d '{
    "filter": {}
    }'