# How to clear data out of an index

**URL:** <https://discuss.elastic.co/t/how-to-clear-data-out-of-an-index/4990>\
**Category:** Elasticsearch\
**Created:** [July 28, 2011, 7:51pm UTC](https://discuss.elastic.co/t/how-to-clear-data-out-of-an-index/4990 "2011-07-28T19:51:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![danpolites](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danpolites/32/2136_2.png) [@danpolites](https://discuss.elastic.co/u/danpolites)\
**Post date:** [July 28, 2011, 7:51pm UTC](https://discuss.elastic.co/t/how-to-clear-data-out-of-an-index/4990/1 "2011-07-28T19:51:36Z")

</div>

How do you clear the data out of an index without deleting that index?  
We are using elasticsearch in a Grails application. During development  
when we end up with rogue objects in the index, we can't remove them  
because they no longer exist in the database. We added an admin  
function that allows us to delete and recreate the index, but we don't  
need to delete the index entirely. We only need to clear it.

---

<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:** [July 28, 2011, 8:52pm UTC](https://discuss.elastic.co/t/how-to-clear-data-out-of-an-index/4990/2 "2011-07-28T20:52:52Z")

</div>

On Thu, 2011-07-28 at 12:51 -0700, danpolites wrote:

> How do you clear the data out of an index without deleting that index?  
> We are using elasticsearch in a Grails application. During development  
> when we end up with rogue objects in the index, we can't remove them  
> because they no longer exist in the database. We added an admin  
> function that allows us to delete and recreate the index, but we don't  
> need to delete the index entirely. We only need to clear it.

Use the delete or delete\_by\_query API

clint

---

<div class="post-metadata">

**Author:** ![Vladimir\_Shkurin](https://avatars.discourse-cdn.com/v4/letter/v/94ad74/32.png) [@Vladimir\_Shkurin](https://discuss.elastic.co/u/Vladimir_Shkurin)\
**Post date:** [July 28, 2011, 9:02pm UTC](https://discuss.elastic.co/t/how-to-clear-data-out-of-an-index/4990/3 "2011-07-28T21:02:12Z")

</div>

Have you seen this? 🙂

[http://www.elasticsearch.org/guide/reference/api/delete-by-query.html](http://www.elasticsearch.org/guide/reference/api/delete-by-query.html)

[http://www.elasticsearch.org/guide/reference/api/delete.html](http://www.elasticsearch.org/guide/reference/api/delete.html)

---

<div class="post-metadata">

**Author:** ![danpolites](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danpolites/32/2136_2.png) [@danpolites](https://discuss.elastic.co/u/danpolites)\
**Post date:** [July 29, 2011, 1:16am UTC](https://discuss.elastic.co/t/how-to-clear-data-out-of-an-index/4990/4 "2011-07-29T01:16:50Z")

</div>

Yes, I have seen those many times ;). Maybe the way I am doing it is  
the best way to do it. I was looking for a more convenient API call  
that would just clear all of the documents in the index without  
deleting the index so that I wouldn't have to setup the index again. I  
suppose a 'delete by query' would work for that, but I'm hesitant to  
use that with the warning that is provided on the page:

"Also, it is not recommended to delete “large chunks of the data in an  
index”, many times, its better to simply reindex into a new index."

Why is this not recommended? We can't reindex to a new index because  
we are creating a new index for every one of our customers and the  
index names are unique to the customer. All of our objects have a  
customer ID field that our search service uses choosing the correct  
index. Again, this is idea of deleting and creating an index to clear  
it out is mainly a convenience thing for development.Hopefully we  
don't have rogue documents in production, but if we do, we need to be  
able to quickly clean the indices because we use elasticsearch results  
for all of our list views in the site and the show pages are MongoDB  
calls. Our current solution of deleting the index and then creating  
that index works OK, but it's really slow at reindexing with large  
amounts of data. Maybe this is more of a best practice sort of  
question. How should this be handled?

On Jul 28, 5:02 pm, Vladimir Shkurin [vshku...@gmail.com](mailto:vshku...@gmail.com) wrote:

> Have you seen this? 🙂
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/delete-by-query.html)
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/delete.html)

---

<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:** [July 29, 2011, 3:59am UTC](https://discuss.elastic.co/t/how-to-clear-data-out-of-an-index/4990/5 "2011-07-29T03:59:57Z")

</div>

I think that you should use one index per customer and create a main alias index on top.

When you need to clean a customer index, then drop it and create it again and add it to the alias.

It will take only some milliseconds. Deleting documents one by one (even with a query) will cost you so much and as you said, you want to remove "quickly" some customer's datas.

Hope this helps  
David

Le 29 juil. 2011 à 03:16, danpolites [dpolites@gmail.com](mailto:dpolites@gmail.com) a écrit :

> Yes, I have seen those many times ;). Maybe the way I am doing it is  
> the best way to do it. I was looking for a more convenient API call  
> that would just clear all of the documents in the index without  
> deleting the index so that I wouldn't have to setup the index again. I  
> suppose a 'delete by query' would work for that, but I'm hesitant to  
> use that with the warning that is provided on the page:
> 
> "Also, it is not recommended to delete “large chunks of the data in an  
> index”, many times, its better to simply reindex into a new index."
> 
> Why is this not recommended? We can't reindex to a new index because  
> we are creating a new index for every one of our customers and the  
> index names are unique to the customer. All of our objects have a  
> customer ID field that our search service uses choosing the correct  
> index. Again, this is idea of deleting and creating an index to clear  
> it out is mainly a convenience thing for development.Hopefully we  
> don't have rogue documents in production, but if we do, we need to be  
> able to quickly clean the indices because we use elasticsearch results  
> for all of our list views in the site and the show pages are MongoDB  
> calls. Our current solution of deleting the index and then creating  
> that index works OK, but it's really slow at reindexing with large  
> amounts of data. Maybe this is more of a best practice sort of  
> question. How should this be handled?
> 
> On Jul 28, 5:02 pm, Vladimir Shkurin [vshku...@gmail.com](mailto:vshku...@gmail.com) wrote:
> 
> > Have you seen this? 🙂
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/delete-by-query.html)
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/delete.html)

---

<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:** [July 29, 2011, 6:29am UTC](https://discuss.elastic.co/t/how-to-clear-data-out-of-an-index/4990/6 "2011-07-29T06:29:42Z")

</div>

The reason for the warning is the fact that when you delete a large part of  
your index, in practice, those documents don't get deleted but only marked  
as deleted in the lucene index. They will eventually get merged out of the  
index (to clean space and optimize the index).

So, if one ends up deleting a large portion of the index, it sometimes makes  
sense to actually reindex the data, as it will create a more optimized  
index. It really depends on the usecase of course.

On Fri, Jul 29, 2011 at 4:16 AM, danpolites [dpolites@gmail.com](mailto:dpolites@gmail.com) wrote:

> Yes, I have seen those many times ;). Maybe the way I am doing it is  
> the best way to do it. I was looking for a more convenient API call  
> that would just clear all of the documents in the index without  
> deleting the index so that I wouldn't have to setup the index again. I  
> suppose a 'delete by query' would work for that, but I'm hesitant to  
> use that with the warning that is provided on the page:
> 
> "Also, it is not recommended to delete “large chunks of the data in an  
> index”, many times, its better to simply reindex into a new index."
> 
> Why is this not recommended? We can't reindex to a new index because  
> we are creating a new index for every one of our customers and the  
> index names are unique to the customer. All of our objects have a  
> customer ID field that our search service uses choosing the correct  
> index. Again, this is idea of deleting and creating an index to clear  
> it out is mainly a convenience thing for development.Hopefully we  
> don't have rogue documents in production, but if we do, we need to be  
> able to quickly clean the indices because we use elasticsearch results  
> for all of our list views in the site and the show pages are MongoDB  
> calls. Our current solution of deleting the index and then creating  
> that index works OK, but it's really slow at reindexing with large  
> amounts of data. Maybe this is more of a best practice sort of  
> question. How should this be handled?
> 
> On Jul 28, 5:02 pm, Vladimir Shkurin [vshku...@gmail.com](mailto:vshku...@gmail.com) wrote:
> 
> > Have you seen this? 🙂
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/delete-by-query.html)
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/delete.html)

---

<div class="post-metadata">

**Author:** ![Michael\_Sokolov](https://avatars.discourse-cdn.com/v4/letter/m/41988e/32.png) [@Michael\_Sokolov](https://discuss.elastic.co/u/Michael_Sokolov)\
**Post date:** [April 6, 2014, 1:00am UTC](https://discuss.elastic.co/t/how-to-clear-data-out-of-an-index/4990/7 "2014-04-06T01:00:06Z")

</div>

This is an old thread, but I have a variation on the same question: I've  
seen various recommendations to drop and recreate indexes rather than  
deleting all documents. I just want to know if there is anything in  
elasticsearch that maps to IndexWriter.deleteAll, since that is an  
efficient way to empty an index without having to recreate it. It would be  
convenient if say deleteByQuery("_:_") were to cause that: does it?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/e55f492c-b027-4a05-9546-dc2b7f7d468d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e55f492c-b027-4a05-9546-dc2b7f7d468d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Itamar\_Syn\_Hershko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itamar_syn_hershko/32/725_2.png) [@Itamar\_Syn\_Hershko](https://discuss.elastic.co/u/Itamar_Syn_Hershko)\
**Post date:** [April 6, 2014, 1:25am UTC](https://discuss.elastic.co/t/how-to-clear-data-out-of-an-index/4990/8 "2014-04-06T01:25:16Z")

</div>

Due to the distributed nature of ES, the equivalent of this would be to  
delete the index and create it again using the same mapping.

--

Itamar Syn-Hershko  
[http://code972.com](http://code972.com) | @synhershko [https://twitter.com/synhershko](https://twitter.com/synhershko)  
Freelance Developer & Consultant  
Author of RavenDB in Action [http://manning.com/synhershko/](http://manning.com/synhershko/)

On Sun, Apr 6, 2014 at 4:00 AM, Michael Sokolov [msokolov@gmail.com](mailto:msokolov@gmail.com) wrote:

> This is an old thread, but I have a variation on the same question: I've  
> seen various recommendations to drop and recreate indexes rather than  
> deleting all documents. I just want to know if there is anything in  
> elasticsearch that maps to IndexWriter.deleteAll, since that is an  
> efficient way to empty an index without having to recreate it. It would be  
> convenient if say deleteByQuery("_:_") were to cause that: does it?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/e55f492c-b027-4a05-9546-dc2b7f7d468d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e55f492c-b027-4a05-9546-dc2b7f7d468d%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/e55f492c-b027-4a05-9546-dc2b7f7d468d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/e55f492c-b027-4a05-9546-dc2b7f7d468d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAHTr4Zt2T%2BpMOyn843Fgffa0LRK\_%2BvfJwjkXtcY9V9pcszw-sg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHTr4Zt2T%2BpMOyn843Fgffa0LRK_%2BvfJwjkXtcY9V9pcszw-sg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:37am UTC](https://discuss.elastic.co/t/how-to-clear-data-out-of-an-index/4990/9 "2017-07-06T01:37:56Z")

</div>


