# How to delete all documents that a field have specific length?

**URL:** <https://discuss.elastic.co/t/how-to-delete-all-documents-that-a-field-have-specific-length/256813>\
**Category:** Elasticsearch\
**Created:** [November 26, 2020, 6:36pm UTC](https://discuss.elastic.co/t/how-to-delete-all-documents-that-a-field-have-specific-length/256813 "2020-11-26T18:36:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ehsan\_sarshar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ehsan_sarshar/32/78444_2.png) [@Ehsan\_sarshar](https://discuss.elastic.co/u/Ehsan_sarshar)\
**Post date:** [November 26, 2020, 6:36pm UTC](https://discuss.elastic.co/t/how-to-delete-all-documents-that-a-field-have-specific-length/256813/1 "2020-11-26T18:36:31Z")

</div>

for example I have following documents  
{  
term: "bo"  
},  
{  
term: "book"  
}

I want to delete all documents that "term" field length is less than 3

---

<div class="post-metadata">

**Author:** ![egalpin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egalpin/32/79772_2.png) [@egalpin](https://discuss.elastic.co/u/egalpin)\
**Post date:** [November 26, 2020, 7:13pm UTC](https://discuss.elastic.co/t/how-to-delete-all-documents-that-a-field-have-specific-length/256813/2 "2020-11-26T19:13:43Z")

</div>

A few approaches I can think of are:

\*Highly recommended that you _query first_ and confirm that the results of your query yield the desired hits.

1. Utilize [delete\_by\_query API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html) in combination with a [script query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html).

2. Reindex your data such that the field you're interested in becomes a multi-field, using a [token\_count](https://www.elastic.co/guide/en/elasticsearch/reference/current/token-count.html) with appropriate analyzer for your data. Then delete by query using the token\_count field directly with no script.

3. [Reindex using an ingest pipeline](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html#reindex-with-an-ingest-pipeline) with [drop processor](https://www.elastic.co/guide/en/elasticsearch/reference/master/drop-processor.html) that filters out docs where the field of interest is less than 3 chars long.

---

<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:** [December 24, 2020, 7:13pm UTC](https://discuss.elastic.co/t/how-to-delete-all-documents-that-a-field-have-specific-length/256813/3 "2020-12-24T19:13:46Z")

</div>

Closed on December 24, 2020, 7:13pm UTC

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