Search code examples
pythonelasticsearch

Delete documents by prefix from elastic search


I have JSON documents with the below format and I would like to delete documents that have any id starting with users_.

{
"id": "users_1",
"name": "Adam"
}

One approach is to get all documents using wildcard then delete their IDs but this would require two requests. Is there a way to delete using a wildcard on a single field?

The solution I've tried is

requests.post(
    'https://<some-project>.us-central1.gcp.cloud.es.io/api/as/v1/engines/<project-name>/_delete_by_query?conflicts=proceed&pretty',
    headers=header, 
    data=json.dumps({
        "query": {
            "match_all": {"ids": "users_*"}
        }
    })
)

Solution

  • Depending on your id mapping, the prefix query should work too:

    .../_delete_by_query
    {
      "query": {
        "prefix": {
          "id": {
            "value": "users_"
          }
        }
      }
    }