# Delete the document in elasticsearch

**URL:** <https://discuss.elastic.co/t/delete-the-document-in-elasticsearch/108383>\
**Category:** Elasticsearch\
**Created:** [November 20, 2017, 12:29pm UTC](https://discuss.elastic.co/t/delete-the-document-in-elasticsearch/108383 "2017-11-20T12:29:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay\_Dabhi](https://avatars.discourse-cdn.com/v4/letter/j/8c91f0/32.png) [@Jay\_Dabhi](https://discuss.elastic.co/u/Jay_Dabhi)\
**Post date:** [November 20, 2017, 12:29pm UTC](https://discuss.elastic.co/t/delete-the-document-in-elasticsearch/108383/1 "2017-11-20T12:29:40Z")

</div>

I want to delete the document in elasticserach by timestamp and one of the custom field "cu\_hostname".  
I want to remove all the documents which are in the specific time stamp which have the value "cu\_hostname=abc"

I have written a query for timestamp as below:  
POST filebeat-perf-1/\_delete\_by\_query  
{  
"query":{  
"range": {  
"@timestamp": {  
"gte": "1510511400000",  
"lte": "1510597799000"  
}  
}  
}  
}  
and deleting the custom field:  
curl -XPOST '10.193.104.42:9200/filebeat-perf-1/\_delete\_by\_query?conflicts=proceed&pretty' -H 'Content-Type: application/json' -d'  
{  
"query": {  
"wildcard": {  
"cu\_hostname": "abc"  
}  
}  
}

How to combine both this query?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 22, 2017, 10:36pm UTC](https://discuss.elastic.co/t/delete-the-document-in-elasticsearch/108383/2 "2017-11-22T22:36:29Z")

</div>

To combine queries you can use [the `bool` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html). To delete all documents that match both queries, you can stick those queries in a `must` clause:

```
{
  "query": {
    "bool": {
      "must": [
        {
          "query": {
            "range": {
              "@timestamp": {
                "gte": "1510511400000",
                "lte": "1510597799000"
              }
            }
          }
        },
        {
          "query": {
            "wildcard": {
              "cu_hostname": "abc"
            }
          }
        }
      ]
    }
  }
}

```

To delete documents that match either one of the queries (not necessarily both, but at least one), you can stick the queries in a `should` clause:

```
{
  "query": {
    "bool": {
      "should": [
        {
          "query": {
            "range": {
              "@timestamp": {
                "gte": "1510511400000",
                "lte": "1510597799000"
              }
            }
          }
        },
        {
          "query": {
            "wildcard": {
              "cu_hostname": "abc"
            }
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 20, 2017, 10:36pm UTC](https://discuss.elastic.co/t/delete-the-document-in-elasticsearch/108383/3 "2017-12-20T22:36:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
