# Removing elements from an array in a document

**URL:** https://discuss.elastic.co/t/removing-elements-from-an-array-in-a-document/10156
**Category:** Elasticsearch
**Created:** [December 21, 2012, 3:54pm UTC](https://discuss.elastic.co/t/removing-elements-from-an-array-in-a-document/10156 "2012-12-21T15:54:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Jayant\_Kerai](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@Jayant\_Kerai](https://discuss.elastic.co/u/Jayant_Kerai)
#### Post date: [December 21, 2012, 3:54pm UTC](https://discuss.elastic.co/t/removing-elements-from-an-array-in-a-document/10156/1 "2012-12-21T15:54:09Z")

</div>

Hi, I'm very new to ES and am evaluating it for my search needs

[http://www.elasticsearch.org/guide/reference/api/update.html](http://www.elasticsearch.org/guide/reference/api/update.html)

shows how to add elements to an array in an existing indexed document.

curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{

```
"script" : "ctx._source.tags += tag",
"params" : {
    "tag" : "blue"
}

```

}'  
The above should add a new element to the list.

Is it possible to remove elements from an array in a similar fashion?  
e.g. the equivalent of

curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{

```
"script" : "ctx._source.tags -= tag",
"params" : {
    "tag" : "blue"
}

```

}'

Aside from reading all the element entries and removing the one 1 want to  
remove and updating the entire element using the \_update endpoint.

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [December 22, 2012, 1:48am UTC](https://discuss.elastic.co/t/removing-elements-from-an-array-in-a-document/10156/2 "2012-12-22T01:48:34Z")

</div>

Tags in your example is an ArrayList[http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#remove(java.lang.Object)](http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#remove(java.lang.Object)).  
So, you can do something like this:

curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{  
"script" : "ctx.\_source.tags.remove(tag)",  
"params" : {  
"tag" : "blue"  
}  
}'

On Friday, December 21, 2012 10:54:09 AM UTC-5, jkerai wrote:

> Hi, I'm very new to ES and am evaluating it for my search needs
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/update.html)
> 
> shows how to add elements to an array in an existing indexed document.
> 
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{
> 
> ```
> "script" : "ctx._source.tags += tag",
> "params" : {
> "tag" : "blue"
> }
> 
> ```
> 
> }'  
> The above should add a new element to the list.
> 
> Is it possible to remove elements from an array in a similar fashion?  
> e.g. the equivalent of
> 
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{
> 
> ```
> "script" : "ctx._source.tags -= tag",
> "params" : {
> "tag" : "blue"
> }
> 
> ```
> 
> }'
> 
> Aside from reading all the element entries and removing the one 1 want to  
> remove and updating the entire element using the \_update endpoint.

--

---

<div class="post-metadata">

### Author: ![Jayant\_Kerai](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@Jayant\_Kerai](https://discuss.elastic.co/u/Jayant_Kerai)
#### Post date: [December 22, 2012, 12:25pm UTC](https://discuss.elastic.co/t/removing-elements-from-an-array-in-a-document/10156/3 "2012-12-22T12:25:32Z")

</div>

Excellent. That works like a dream.  
And I appreciate you correcting my terminology. Thanks Igor!

Just to elaborate, my actual example is a little different from what I  
posted but your solution works.

My example:

curl -XPUT '[http://localhost:9200/myindex/mytype/12](http://localhost:9200/myindex/mytype/12)' -d '{  
"Name": "Entity 1",  
"Description": "Jay Desc 1",  
"Entities": [  
{  
"CategoryID": "1",  
"CategoryName": "Office",  
"EntityID": "1",  
"EntityName": "UK"  
},  
{  
"CategoryID": "1",  
"CategoryName": "Office",  
"EntityID": "2",  
"EntityName": "South Africa"  
},  
{  
"CategoryID": "2",  
"CategoryName": "Team",  
"EntityID": "3",  
"EntityName": "Accounts"  
},  
{  
"CategoryID": "2",  
"CategoryName": "Team",  
"EntityID": "4",  
"EntityName": "Development"  
}  
]  
}'

curl -XPOST '[http://localhost:9200/myindex/mytype/12/\_update](http://localhost:9200/myindex/mytype/12/_update)' -d  
'{  
"script" : "ctx.\_source.Entities.remove(Entities)",  
"params" : {  
"Entities":  
{  
"CategoryID": "2",  
"CategoryName": "Team",  
"EntityID": "4",  
"EntityName": "Development"  
}  
}  
}'

On Friday, 21 December 2012 15:54:09 UTC, jkerai wrote:

> Hi, I'm very new to ES and am evaluating it for my search needs
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/update.html)
> 
> shows how to add elements to an array in an existing indexed document.
> 
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{
> 
> ```
> "script" : "ctx._source.tags += tag",
> "params" : {
> "tag" : "blue"
> }
> 
> ```
> 
> }'  
> The above should add a new element to the list.
> 
> Is it possible to remove elements from an array in a similar fashion?  
> e.g. the equivalent of
> 
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{
> 
> ```
> "script" : "ctx._source.tags -= tag",
> "params" : {
> "tag" : "blue"
> }
> 
> ```
> 
> }'
> 
> Aside from reading all the element entries and removing the one 1 want to  
> remove and updating the entire element using the \_update endpoint.

--

---

<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: [July 6, 2017, 2:58am UTC](https://discuss.elastic.co/t/removing-elements-from-an-array-in-a-document/10156/4 "2017-07-06T02:58:39Z")

</div>


