# What is the equivalent of the removeAll function in Painless

**URL:** https://discuss.elastic.co/t/what-is-the-equivalent-of-the-removeall-function-in-painless/138308
**Category:** Elasticsearch
**Created:** [July 3, 2018, 5:49am UTC](https://discuss.elastic.co/t/what-is-the-equivalent-of-the-removeall-function-in-painless/138308 "2018-07-03T05:49:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Kvin](https://avatars.discourse-cdn.com/v4/letter/k/e99b99/32.png) [@Kvin](https://discuss.elastic.co/u/Kvin)
#### Post date: [July 3, 2018, 5:49am UTC](https://discuss.elastic.co/t/what-is-the-equivalent-of-the-removeall-function-in-painless/138308/1 "2018-07-03T05:49:59Z")

</div>

We had the following line of Groovy script in 5.X:

> "inline":"ctx.\_source.all\_phone.removeAll{it.phone\_number == params.remove\_id}

It throws the following error in 6.3.0:

> {  
> "error": {  
> "root\_cause": [  
> {  
> "type": "script\_exception",  
> "reason": "compile error",  
> "script\_stack": [  
> "... ource.all\_phone.removeAll{it.phone\_number == param ...",  
> " ^---- HERE"  
> ],  
> "script": "ctx.\_source.all\_phone.removeAll{it.phone\_number == params.remove\_id}",  
> "lang": "painless"  
> }  
> ],  
> "type": "script\_exception",  
> "reason": "compile error",  
> "script\_stack": [  
> "... ource.all\_phone.removeAll{it.phone\_number == param ...",  
> " ^---- HERE"  
> ],  
> "script": "ctx.\_source.all\_phone.removeAll{it.phone\_number == params.remove\_id}",  
> "lang": "painless",  
> "caused\_by": {  
> "type": "illegal\_argument\_exception",  
> "reason": "invalid sequence of tokens near ['{'].",  
> "caused\_by": {  
> "type": "no\_viable\_alt\_exception",  
> "reason": null  
> }  
> }  
> },  
> "status": 500  
> }

What I want to do is: remove elements from a nested field which match a given criteria.

---

<div class="post-metadata">

### Author: ![Kvin](https://avatars.discourse-cdn.com/v4/letter/k/e99b99/32.png) [@Kvin](https://discuss.elastic.co/u/Kvin)
#### Post date: [July 3, 2018, 6:35am UTC](https://discuss.elastic.co/t/what-is-the-equivalent-of-the-removeall-function-in-painless/138308/2 "2018-07-03T06:35:42Z")

</div>

I'm not sure if this is what I've originally wanted, but I think it's a nice workaround:

> "source":"ctx.\_source.all\_phone = ctx.\_source.all\_phone.stream().filter(x -\> x.phone\_number != params.remove\_id).collect(Collectors.toList())"

So this basically collects every item from the array which matches the given condition, then assigns the result back to the original variable.

Source: [Elasticsearch: How do I remove a nested object element? - Stack Overflow](https://stackoverflow.com/questions/44679876/elasticsearch-how-do-i-remove-a-nested-object-element)

---

<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 31, 2018, 6:49am UTC](https://discuss.elastic.co/t/what-is-the-equivalent-of-the-removeall-function-in-painless/138308/3 "2018-07-31T06:49:09Z")

</div>

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