# Delete a field matching a regex

**URL:** <https://discuss.elastic.co/t/delete-a-field-matching-a-regex/135541>\
**Category:** Elasticsearch\
**Created:** [June 12, 2018, 1:09pm UTC](https://discuss.elastic.co/t/delete-a-field-matching-a-regex/135541 "2018-06-12T13:09:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GregoryB-T](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregoryb-t/32/28564_2.png) [@GregoryB-T](https://discuss.elastic.co/u/GregoryB-T)\
**Post date:** [June 12, 2018, 1:09pm UTC](https://discuss.elastic.co/t/delete-a-field-matching-a-regex/135541/1 "2018-06-12T13:09:29Z")

</div>

Hi

is it possible to delete a field in all the document of an index if he match to a regex ?

Thanks and Regards

---

<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:** [June 12, 2018, 8:45pm UTC](https://discuss.elastic.co/t/delete-a-field-matching-a-regex/135541/2 "2018-06-12T20:45:57Z")

</div>

Sounds like a work for [Update By Query](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs-update-by-query.html) with a [regex](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-regexp-query.html) query.

Please note that in Elasticsearch it's not possible to delete a field. Operations are atomic on the document level, so deleting a field is basically deleting a document in reindexing the same document without the field.

---

<div class="post-metadata">

**Author:** ![GregoryB-T](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregoryb-t/32/28564_2.png) [@GregoryB-T](https://discuss.elastic.co/u/GregoryB-T)\
**Post date:** [June 13, 2018, 8:12am UTC](https://discuss.elastic.co/t/delete-a-field-matching-a-regex/135541/3 "2018-06-13T08:12:43Z")

</div>

Thanks for the quick response,

I've read the link you give me, but the thing that I don't see how to do is to perform the regex not on a specific value of a field but on the field name.

by example : if I want to remove all the fields that begin by extra\_data\_other

thanks

---

<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:** [June 13, 2018, 2:13pm UTC](https://discuss.elastic.co/t/delete-a-field-matching-a-regex/135541/4 "2018-06-13T14:13:34Z")

</div>

This will reindex all documents from the index `src` to the index `dst` while removing all fields that start with `b`:

```auto
PUT src/doc/1
{
  "foo": 1,
  "bar": 2,
  "baz": 3
}

POST _reindex
{
  "source": {
    "index": "src"
  },
  "dest": {
    "index": "dst"
  },
  "script": {
    "source": "ctx._source.keySet().stream().filter(s -> s.startsWith('b')).collect(Collectors.toList()).forEach(k -> ctx._source.remove(k))",
    "lang": "painless"
  }
}

```

---

<div class="post-metadata">

**Author:** ![GregoryB-T](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregoryb-t/32/28564_2.png) [@GregoryB-T](https://discuss.elastic.co/u/GregoryB-T)\
**Post date:** [June 13, 2018, 3:01pm UTC](https://discuss.elastic.co/t/delete-a-field-matching-a-regex/135541/5 "2018-06-13T15:01:50Z")

</div>

Ho thank you ,

I miss that from the painless doc 🤩

---

<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 11, 2018, 3:01pm UTC](https://discuss.elastic.co/t/delete-a-field-matching-a-regex/135541/6 "2018-07-11T15:01:54Z")

</div>

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