# Delete document when not in specified indexes

**URL:** <https://discuss.elastic.co/t/delete-document-when-not-in-specified-indexes/5823>\
**Category:** Elasticsearch\
**Created:** [November 9, 2011, 1:22am UTC](https://discuss.elastic.co/t/delete-document-when-not-in-specified-indexes/5823 "2011-11-09T01:22:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kunal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kunal/32/3079_2.png) [@Kunal](https://discuss.elastic.co/u/Kunal)\
**Post date:** [November 9, 2011, 1:22am UTC](https://discuss.elastic.co/t/delete-document-when-not-in-specified-indexes/5823/1 "2011-11-09T01:22:13Z")

</div>

Is there a way, using delete by query, to delete all documents that  
are not in certain indexes?

So I might have a bunch of documents

[http://localhost:9200/index1/mytype/1](http://localhost:9200/index1/mytype/1)  
[http://localhost:9200/index2/mytype/1](http://localhost:9200/index2/mytype/1)  
[http://localhost:9200/index3/mytype/1](http://localhost:9200/index3/mytype/1)  
[http://localhost:9200/index4/mytype/1](http://localhost:9200/index4/mytype/1)  
...  
[http://localhost:9200/index99/mytype/1](http://localhost:9200/index99/mytype/1)

... and I want to execute a query that deletes document 1, of type  
"mytype", when it's not in index3 or index4. Is that possible without  
also storing the index as another term in the document itself?

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [November 9, 2011, 8:56am UTC](https://discuss.elastic.co/t/delete-document-when-not-in-specified-indexes/5823/2 "2011-11-09T08:56:59Z")

</div>

> ... and I want to execute a query that deletes document 1, of type  
> "mytype", when it's not in index3 or index4. Is that possible without  
> also storing the index as another term in the document itself?

You would need to index the \_index value, which you can enable in the  
mapping:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

clint

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 9, 2011, 11:28am UTC](https://discuss.elastic.co/t/delete-document-when-not-in-specified-indexes/5823/3 "2011-11-09T11:28:04Z")

</div>

One option is to execute the delete against all indices, except for index3  
and index4.

On Wed, Nov 9, 2011 at 3:22 AM, Kunal [cvkunal@gmail.com](mailto:cvkunal@gmail.com) wrote:

> Is there a way, using delete by query, to delete all documents that  
> are not in certain indexes?
> 
> So I might have a bunch of documents
> 
> [http://localhost:9200/index1/mytype/1](http://localhost:9200/index1/mytype/1)  
> [http://localhost:9200/index2/mytype/1](http://localhost:9200/index2/mytype/1)  
> [http://localhost:9200/index3/mytype/1](http://localhost:9200/index3/mytype/1)  
> [http://localhost:9200/index4/mytype/1](http://localhost:9200/index4/mytype/1)  
> ...  
> [http://localhost:9200/index99/mytype/1](http://localhost:9200/index99/mytype/1)
> 
> ... and I want to execute a query that deletes document 1, of type  
> "mytype", when it's not in index3 or index4. Is that possible without  
> also storing the index as another term in the document itself?

---

<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, 3:49am UTC](https://discuss.elastic.co/t/delete-document-when-not-in-specified-indexes/5823/4 "2017-07-06T03:49:18Z")

</div>


