# How to remove many fields by script?

**URL:** https://discuss.elastic.co/t/how-to-remove-many-fields-by-script/128992
**Category:** Elasticsearch
**Created:** [April 22, 2018, 7:31am UTC](https://discuss.elastic.co/t/how-to-remove-many-fields-by-script/128992 "2018-04-22T07:31:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mh20](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mh20](https://discuss.elastic.co/u/mh20)
#### Post date: [April 22, 2018, 7:31am UTC](https://discuss.elastic.co/t/how-to-remove-many-fields-by-script/128992/1 "2018-04-22T07:31:12Z")

</div>

Hi, I'm using script below to remove multiple fields from elasticsearch:  
"script" : "ctx.\_source.remove('new\_field1'); ctx.\_source.remove('new\_field2')"  
When there are many fields that should be removed, this exception happens:

compile error  
illegal\_argument\_exception  
Scripts may be no longer than 16384 characters.

So how can I remove many fields by scripting, without script length overflow?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [April 22, 2018, 8:37am UTC](https://discuss.elastic.co/t/how-to-remove-many-fields-by-script/128992/2 "2018-04-22T08:37:15Z")

</div>

May create a for loop in painless and pass an array of fields as params?

I'm curious about what you are doing though. Why do you need to remove so many fields?  
Wouldn't reindex be better?

---

<div class="post-metadata">

### Author: ![mh20](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mh20](https://discuss.elastic.co/u/mh20)
#### Post date: [April 24, 2018, 12:04pm UTC](https://discuss.elastic.co/t/how-to-remove-many-fields-by-script/128992/3 "2018-04-24T12:04:29Z")

</div>

How can I use script of removing fields with param of multiple fields?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [April 24, 2018, 12:43pm UTC](https://discuss.elastic.co/t/how-to-remove-many-fields-by-script/128992/4 "2018-04-24T12:43:07Z")

</div>

I never did that. So I don't know. You can in worse case send a param like "1,2,3,4" then split the string in the script on comma and for each Id do whatever you need.

Just an idea.

---

<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: [May 22, 2018, 12:43pm UTC](https://discuss.elastic.co/t/how-to-remove-many-fields-by-script/128992/5 "2018-05-22T12:43:11Z")

</div>

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