# Delete document in ElasticSearch Output Plugin using Script

**URL:** https://discuss.elastic.co/t/delete-document-in-elasticsearch-output-plugin-using-script/126128
**Category:** Logstash
**Created:** [March 29, 2018, 5:01pm UTC](https://discuss.elastic.co/t/delete-document-in-elasticsearch-output-plugin-using-script/126128 "2018-03-29T17:01:52Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![msugas19](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/msugas19/32/26627_2.png) [@msugas19](https://discuss.elastic.co/u/msugas19)
#### Post date: [March 29, 2018, 5:01pm UTC](https://discuss.elastic.co/t/delete-document-in-elasticsearch-output-plugin-using-script/126128/1 "2018-03-29T17:01:52Z")

</div>

I am trying to delete documents using a scripted query in Elastic Search through Logstash, however whenever I do I get the following error:

```
[2018-03-28T22:06:17,681][ERROR][logstash.outputs.elasticsearch] Encountered a retryable error. Will Retry with exponential backoff {:code=>400, :url=>"http://MY_ES_URL/_bulk"}

```

I am pointing to the correct index, correct type, and the scripted query returns correctly whenever I run with a regular script tag as seen below:

```
GET MY_INDEX/MY_TYPE/_search
{
    "query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : {
                        "inline": "doc[\"MY_FIELD.keyword\"][0].toLowerCase().equals(\"1234\")",
                        "lang": "painless"
                     }
                }
            }
        }
    }
}

```

Here is my ElasticSearch output plugin configuration:

```
   elasticsearch {
        hosts => ["MY_ES_URL"]
        action => "delete"
        index => "MY_INDEX"
        document_type => "MY_TYPE"
        script_lang => "painless"
        script_type => "inline"
        script => '
            doc[\"MY_FIELD.keyword\"][0].toLowerCase().equals(params.event.get("MY_FIELD").toString().toLowerCase())
        ' 
    }

```

It seems as if it is a problem with Elastic's output trying to use the BULK API, however I do not know how to fix this without resorting to the HTTP plugin and \_delete\_by\_query. I am using ElasticSearch 5.4 with Logstash 5.6 and the latest ElasticSearch output plugin.

---

<div class="post-metadata">

### Author: ![msugas19](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/msugas19/32/26627_2.png) [@msugas19](https://discuss.elastic.co/u/msugas19)
#### Post date: [March 29, 2018, 5:07pm UTC](https://discuss.elastic.co/t/delete-document-in-elasticsearch-output-plugin-using-script/126128/2 "2018-03-29T17:07:02Z")

</div>

I tried to replicate what the output plugin is doing through the BULK API and I ran the following Query:

```
POST _bulk
{
  "delete": {
    "_index": "MY_INDEX",
    "_type": "MY_TYPE",
    "query": {
      "bool": {
        "must": {
          "script": {
            "script": {
              "inline": "doc[\"MY_FIELD.keyword\"][0].toLowerCase().equals(\"1234\")",
              "lang": "painless"
            }
          }
        }
      }
    }
  }}

```

The error I am getting is this:

```
{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Malformed action/metadata line [1], expected a simple value for field [query] but found [START_OBJECT]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Malformed action/metadata line [1], expected a simple value for field [query] but found [START_OBJECT]"
  },
  "status": 400
}
```

---

<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: [April 26, 2018, 5:13pm UTC](https://discuss.elastic.co/t/delete-document-in-elasticsearch-output-plugin-using-script/126128/3 "2018-04-26T17:13:28Z")

</div>

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