# Removing items from an ArrayList with script

**URL:** https://discuss.elastic.co/t/removing-items-from-an-arraylist-with-script/203729
**Category:** Elasticsearch
**Created:** [October 16, 2019, 1:45am UTC](https://discuss.elastic.co/t/removing-items-from-an-arraylist-with-script/203729 "2019-10-16T01:45:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Dennis\_Monsewicz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dennis_monsewicz/32/53037_2.png) [@Dennis\_Monsewicz](https://discuss.elastic.co/u/Dennis_Monsewicz)
#### Post date: [October 16, 2019, 1:45am UTC](https://discuss.elastic.co/t/removing-items-from-an-arraylist-with-script/203729/1 "2019-10-16T01:45:02Z")

</div>

I'm attempting to try to remove items from an ArrayList on a doc, but not having any luck.

```
        source: "for(item in params.tags) { if(ctx._source.tag_list.indexOf(item) !== -1) { ctx._source.tag_list.remove(item) } }",
        params: {
          tags: tags
        }

```

The code runs, but the indexOf always returns -1 even though the document contains the tags I am trying to remove

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [October 16, 2019, 1:00pm UTC](https://discuss.elastic.co/t/removing-items-from-an-arraylist-with-script/203729/2 "2019-10-16T13:00:50Z")

</div>

can you provide a fully reproducible minimal example including index and document creation?

--Alex

---

<div class="post-metadata">

### Author: ![Dennis\_Monsewicz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dennis_monsewicz/32/53037_2.png) [@Dennis\_Monsewicz](https://discuss.elastic.co/u/Dennis_Monsewicz)
#### Post date: [October 16, 2019, 1:24pm UTC](https://discuss.elastic.co/t/removing-items-from-an-arraylist-with-script/203729/3 "2019-10-16T13:24:12Z")

</div>

@spinscale sure.

Document already indexed in my ES index:

```
{
    "_index": "my_index_development",
    "_type": "my_type",
    "_id": "1",
    "_version": 20,
    "_routing": "2",
    "found": true,
    "_source": {
        "first_name": "hello",
        "last_name": "world",
        "full_name": "hello world",
        "email": "fu@bar.com",
        "created_at": "2019-10-15T19:45:52.000Z",
        "user_id": 3,
        "account_id": 3,
        "lists": [],
        "tag_list": [
            [
                "challenger",
                "ram"
            ]
        ]
    }
} 

```

What I am trying to do is remove tags from the `tag_list` attribute.

```
POST /my_index/my_type/1/_update
{
    "script": {
        source: "for(item in params.tags) { if(ctx._source.tag_list.indexOf(item) !== -1) { ctx._source.tag_list.remove(item) } }",
        params: {
          tags: ['challenger']
        }
     }
}
```

---

<div class="post-metadata">

### Author: ![Dennis\_Monsewicz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dennis_monsewicz/32/53037_2.png) [@Dennis\_Monsewicz](https://discuss.elastic.co/u/Dennis_Monsewicz)
#### Post date: [October 16, 2019, 1:48pm UTC](https://discuss.elastic.co/t/removing-items-from-an-arraylist-with-script/203729/4 "2019-10-16T13:48:44Z")

</div>

I figured out my problem. It was working all along, but the `tag_list` attribute for this document got saved with a nested array which is why I kept running into problems.

The for loop works like its supposed too

---

<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: [November 13, 2019, 1:58pm UTC](https://discuss.elastic.co/t/removing-items-from-an-arraylist-with-script/203729/5 "2019-11-13T13:58:16Z")

</div>

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