# How to delete all available Documents from index Type?

**URL:** <https://discuss.elastic.co/t/how-to-delete-all-available-documents-from-index-type/7453>\
**Category:** Elasticsearch\
**Created:** [April 24, 2012, 1:52pm UTC](https://discuss.elastic.co/t/how-to-delete-all-available-documents-from-index-type/7453 "2012-04-24T13:52:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_4](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@Sam\_4](https://discuss.elastic.co/u/Sam_4)\
**Post date:** [April 24, 2012, 1:52pm UTC](https://discuss.elastic.co/t/how-to-delete-all-available-documents-from-index-type/7453/1 "2012-04-24T13:52:52Z")

</div>

Hi

My index contain many index Type,I want to delete all document from given  
index type, i have try on following query:

TermQueryBuilder termQueryBuilder = new  
TermQueryBuilder("\_type", indexType);

```
    DeleteByQueryResponse response = client
                                     .prepareDeleteByQuery(indexName)
                                     .setQuery(termQueryBuilder)
                                     .execute()
                                     .actionGet();

```

where indexName represent the index name and type is inexType which came  
from other side.when i execute this query,i assume that indextype is  
deleted as well as all document which depend on indexType,but when i try to  
index a document with same indexType and document id,i got the following  
error:

org.elasticsearch.index.engine.DocumentAlreadyExistsEngineException:  
[idx\_masterdatabase][4] [type\_testtable][3]: document already exists  
at  
org.elasticsearch.index.engine.robin.RobinEngine.innerCreate(RobinEngine.java:349)  
at  
org.elasticsearch.index.engine.robin.RobinEngine.create(RobinEngine.java:266)  
at  
org.elasticsearch.index.shard.service.InternalIndexShard.create(InternalIndexShard.java:272)  
at  
org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:136)  
at  
org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:418)  
at  
org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.access$100(TransportShardReplicationOperationAction.java:233)  
at  
org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:331)  
at  
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
at  
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
at java.lang.Thread.run(Thread.java:662)

---

<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:** [April 27, 2012, 8:49am UTC](https://discuss.elastic.co/t/how-to-delete-all-available-documents-from-index-type/7453/2 "2012-04-27T08:49:49Z")

</div>

In the Java API, you have the option ot delete a mapping with its data,  
client.admin().indices().prepareDeleteMapping

On Tue, Apr 24, 2012 at 4:52 PM, sam [mishra.sameek@gmail.com](mailto:mishra.sameek@gmail.com) wrote:

> Hi
> 
> My index contain many index Type,I want to delete all document from given  
> index type, i have try on following query:
> 
> TermQueryBuilder termQueryBuilder = new  
> TermQueryBuilder("\_type", indexType);
> 
> ```
> DeleteByQueryResponse response = client
> .prepareDeleteByQuery(indexName)
> .setQuery(termQueryBuilder)
> .execute()
> .actionGet();
> 
> ```
> 
> where indexName represent the index name and type is inexType which came  
> from other side.when i execute this query,i assume that indextype is  
> deleted as well as all document which depend on indexType,but when i try to  
> index a document with same indexType and document id,i got the following  
> error:
> 
> org.elasticsearch.index.engine.DocumentAlreadyExistsEngineException:  
> [idx\_masterdatabase][4] [type\_testtable][3]: document already exists  
> at  
> org.elasticsearch.index.engine.robin.RobinEngine.innerCreate(RobinEngine.java:349)  
> at  
> org.elasticsearch.index.engine.robin.RobinEngine.create(RobinEngine.java:266)  
> at  
> org.elasticsearch.index.shard.service.InternalIndexShard.create(InternalIndexShard.java:272)  
> at  
> org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:136)  
> at  
> org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:418)  
> at  
> org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.access$100(TransportShardReplicationOperationAction.java:233)  
> at  
> org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:331)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> at java.lang.Thread.run(Thread.java:662)

---

<div class="post-metadata">

**Author:** ![Mxims\_2](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mxims\_2](https://discuss.elastic.co/u/Mxims_2)\
**Post date:** [January 18, 2013, 11:39pm UTC](https://discuss.elastic.co/t/how-to-delete-all-available-documents-from-index-type/7453/3 "2013-01-18T23:39:34Z")

</div>

Hi -  
Is there a method (Java API) to check whether the TYPE exists before we try  
to delete its mapping?

On Friday, April 27, 2012 1:49:49 AM UTC-7, kimchy wrote:

> In the Java API, you have the option ot delete a mapping with its data,  
> client.admin().indices().prepareDeleteMapping
> 
> On Tue, Apr 24, 2012 at 4:52 PM, sam \<[mishra...@gmail.com](mailto:mishra...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hi
> > 
> > My index contain many index Type,I want to delete all document from given  
> > index type, i have try on following query:
> > 
> > TermQueryBuilder termQueryBuilder = new  
> > TermQueryBuilder("\_type", indexType);
> > 
> > ```
> > DeleteByQueryResponse response = client
> > .prepareDeleteByQuery(indexName)
> > .setQuery(termQueryBuilder)
> > .execute()
> > .actionGet();
> > 
> > ```
> > 
> > where indexName represent the index name and type is inexType which came  
> > from other side.when i execute this query,i assume that indextype is  
> > deleted as well as all document which depend on indexType,but when i try to  
> > index a document with same indexType and document id,i got the following  
> > error:
> > 
> > org.elasticsearch.index.engine.DocumentAlreadyExistsEngineException:  
> > [idx\_masterdatabase][4] [type\_testtable][3]: document already exists  
> > at  
> > org.elasticsearch.index.engine.robin.RobinEngine.innerCreate(RobinEngine.java:349)  
> > at  
> > org.elasticsearch.index.engine.robin.RobinEngine.create(RobinEngine.java:266)  
> > at  
> > org.elasticsearch.index.shard.service.InternalIndexShard.create(InternalIndexShard.java:272)  
> > at  
> > org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:136)  
> > at  
> > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:418)  
> > at  
> > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.access$100(TransportShardReplicationOperationAction.java:233)  
> > at  
> > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:331)  
> > at  
> > java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> > at  
> > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> > at java.lang.Thread.run(Thread.java:662)

--

---

<div class="post-metadata">

**Author:** ![Edward\_Smit](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Edward\_Smit](https://discuss.elastic.co/u/Edward_Smit)\
**Post date:** [January 19, 2013, 11:50am UTC](https://discuss.elastic.co/t/how-to-delete-all-available-documents-from-index-type/7453/4 "2013-01-19T11:50:53Z")

</div>

You could fire a query for documents of the TYPE.

Op zaterdag 19 januari 2013 00:39:34 UTC+1 schreef Mxims het volgende:

> Hi -  
> Is there a method (Java API) to check whether the TYPE exists before we  
> try to delete its mapping?
> 
> On Friday, April 27, 2012 1:49:49 AM UTC-7, kimchy wrote:
> 
> > In the Java API, you have the option ot delete a mapping with its data,  
> > client.admin().indices().prepareDeleteMapping
> > 
> > On Tue, Apr 24, 2012 at 4:52 PM, sam [mishra...@gmail.com](mailto:mishra...@gmail.com) wrote:
> > 
> > > Hi
> > > 
> > > My index contain many index Type,I want to delete all document from  
> > > given index type, i have try on following query:
> > > 
> > > TermQueryBuilder termQueryBuilder = new  
> > > TermQueryBuilder("\_type", indexType);
> > > 
> > > ```
> > > DeleteByQueryResponse response = client
> > > .prepareDeleteByQuery(indexName)
> > > .setQuery(termQueryBuilder)
> > > .execute()
> > > .actionGet();
> > > 
> > > ```
> > > 
> > > where indexName represent the index name and type is inexType which came  
> > > from other side.when i execute this query,i assume that indextype is  
> > > deleted as well as all document which depend on indexType,but when i try to  
> > > index a document with same indexType and document id,i got the following  
> > > error:
> > > 
> > > org.elasticsearch.index.engine.DocumentAlreadyExistsEngineException:  
> > > [idx\_masterdatabase][4] [type\_testtable][3]: document already exists  
> > > at  
> > > org.elasticsearch.index.engine.robin.RobinEngine.innerCreate(RobinEngine.java:349)  
> > > at  
> > > org.elasticsearch.index.engine.robin.RobinEngine.create(RobinEngine.java:266)  
> > > at  
> > > org.elasticsearch.index.shard.service.InternalIndexShard.create(InternalIndexShard.java:272)  
> > > at  
> > > org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:136)  
> > > at  
> > > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:418)  
> > > at  
> > > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.access$100(TransportShardReplicationOperationAction.java:233)  
> > > at  
> > > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:331)  
> > > at  
> > > java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> > > at  
> > > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> > > at java.lang.Thread.run(Thread.java:662)

--

---

<div class="post-metadata">

**Author:** ![Edward\_Smit](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Edward\_Smit](https://discuss.elastic.co/u/Edward_Smit)\
**Post date:** [January 19, 2013, 11:54am UTC](https://discuss.elastic.co/t/how-to-delete-all-available-documents-from-index-type/7453/5 "2013-01-19T11:54:12Z")

</div>

Hmm, that would only work if there exist document of the TYPE. See "Using  
Java API, how do I find out whether a given type exists in the index?" for  
a better answer.

Op zaterdag 19 januari 2013 12:50:53 UTC+1 schreef Edward Smit het volgende:

> You could fire a query for documents of the TYPE.
> 
> Op zaterdag 19 januari 2013 00:39:34 UTC+1 schreef Mxims het volgende:
> 
> > Hi -  
> > Is there a method (Java API) to check whether the TYPE exists before we  
> > try to delete its mapping?
> > 
> > On Friday, April 27, 2012 1:49:49 AM UTC-7, kimchy wrote:
> > 
> > > In the Java API, you have the option ot delete a mapping with its data,  
> > > client.admin().indices().prepareDeleteMapping
> > > 
> > > On Tue, Apr 24, 2012 at 4:52 PM, sam [mishra...@gmail.com](mailto:mishra...@gmail.com) wrote:
> > > 
> > > > Hi
> > > > 
> > > > My index contain many index Type,I want to delete all document from  
> > > > given index type, i have try on following query:
> > > > 
> > > > TermQueryBuilder termQueryBuilder = new  
> > > > TermQueryBuilder("\_type", indexType);
> > > > 
> > > > ```
> > > > DeleteByQueryResponse response = client
> > > > 
> > > > ```
> > > > 
> > > > .prepareDeleteByQuery(indexName)  
> > > > .setQuery(termQueryBuilder)  
> > > > .execute()  
> > > > .actionGet();
> > > > 
> > > > where indexName represent the index name and type is inexType which  
> > > > came from other side.when i execute this query,i assume that indextype is  
> > > > deleted as well as all document which depend on indexType,but when i try to  
> > > > index a document with same indexType and document id,i got the following  
> > > > error:
> > > > 
> > > > org.elasticsearch.index.engine.DocumentAlreadyExistsEngineException:  
> > > > [idx\_masterdatabase][4] [type\_testtable][3]: document already exists  
> > > > at  
> > > > org.elasticsearch.index.engine.robin.RobinEngine.innerCreate(RobinEngine.java:349)  
> > > > at  
> > > > org.elasticsearch.index.engine.robin.RobinEngine.create(RobinEngine.java:266)  
> > > > at  
> > > > org.elasticsearch.index.shard.service.InternalIndexShard.create(InternalIndexShard.java:272)  
> > > > at  
> > > > org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:136)  
> > > > at  
> > > > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:418)  
> > > > at  
> > > > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.access$100(TransportShardReplicationOperationAction.java:233)  
> > > > at  
> > > > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:331)  
> > > > at  
> > > > java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> > > > at  
> > > > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> > > > at java.lang.Thread.run(Thread.java:662)

--

---

<div class="post-metadata">

**Author:** ![Mxims\_2](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mxims\_2](https://discuss.elastic.co/u/Mxims_2)\
**Post date:** [January 20, 2013, 1:37am UTC](https://discuss.elastic.co/t/how-to-delete-all-available-documents-from-index-type/7453/6 "2013-01-20T01:37:04Z")

</div>

Thank you Edward for your help!

I did refer to the answer on the other post.

Renjith from Seattle

On Saturday, January 19, 2013 3:54:12 AM UTC-8, Edward Smit wrote:

> Hmm, that would only work if there exist document of the TYPE. See "Using  
> Java API, how do I find out whether a given type exists in the index?" for  
> a better answer.
> 
> Op zaterdag 19 januari 2013 12:50:53 UTC+1 schreef Edward Smit het  
> volgende:
> 
> > You could fire a query for documents of the TYPE.
> > 
> > Op zaterdag 19 januari 2013 00:39:34 UTC+1 schreef Mxims het volgende:
> > 
> > > Hi -  
> > > Is there a method (Java API) to check whether the TYPE exists before we  
> > > try to delete its mapping?
> > > 
> > > On Friday, April 27, 2012 1:49:49 AM UTC-7, kimchy wrote:
> > > 
> > > > In the Java API, you have the option ot delete a mapping with its data,  
> > > > client.admin().indices().prepareDeleteMapping
> > > > 
> > > > On Tue, Apr 24, 2012 at 4:52 PM, sam [mishra...@gmail.com](mailto:mishra...@gmail.com) wrote:
> > > > 
> > > > > Hi
> > > > > 
> > > > > My index contain many index Type,I want to delete all document from  
> > > > > given index type, i have try on following query:
> > > > > 
> > > > > TermQueryBuilder termQueryBuilder = new  
> > > > > TermQueryBuilder("\_type", indexType);
> > > > > 
> > > > > ```
> > > > > DeleteByQueryResponse response = client
> > > > > 
> > > > > ```
> > > > > 
> > > > > .prepareDeleteByQuery(indexName)  
> > > > > .setQuery(termQueryBuilder)  
> > > > > .execute()  
> > > > > .actionGet();
> > > > > 
> > > > > where indexName represent the index name and type is inexType which  
> > > > > came from other side.when i execute this query,i assume that indextype is  
> > > > > deleted as well as all document which depend on indexType,but when i try to  
> > > > > index a document with same indexType and document id,i got the following  
> > > > > error:
> > > > > 
> > > > > org.elasticsearch.index.engine.DocumentAlreadyExistsEngineException:  
> > > > > [idx\_masterdatabase][4] [type\_testtable][3]: document already exists  
> > > > > at  
> > > > > org.elasticsearch.index.engine.robin.RobinEngine.innerCreate(RobinEngine.java:349)  
> > > > > at  
> > > > > org.elasticsearch.index.engine.robin.RobinEngine.create(RobinEngine.java:266)  
> > > > > at  
> > > > > org.elasticsearch.index.shard.service.InternalIndexShard.create(InternalIndexShard.java:272)  
> > > > > at  
> > > > > org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:136)  
> > > > > at  
> > > > > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:418)  
> > > > > at  
> > > > > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.access$100(TransportShardReplicationOperationAction.java:233)  
> > > > > at  
> > > > > org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:331)  
> > > > > at  
> > > > > java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> > > > > at  
> > > > > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> > > > > at java.lang.Thread.run(Thread.java:662)

--

---

<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, 2:55am UTC](https://discuss.elastic.co/t/how-to-delete-all-available-documents-from-index-type/7453/7 "2017-07-06T02:55:31Z")

</div>


