# Java API - delete all documents within the specific index and the specific type

**URL:** <https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681>\
**Category:** Elasticsearch\
**Created:** [June 22, 2011, 7:28am UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681 "2011-06-22T07:28:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slava\_G](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@Slava\_G](https://discuss.elastic.co/u/Slava_G)\
**Post date:** [June 22, 2011, 7:28am UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/1 "2011-06-22T07:28:49Z")

</div>

Hi,  
I'm trying to delete all documents within specific index and type or  
to delete entire index, using this piece of code:

DeleteResponse response = client.prepareDelete().setIndex(indexKey)  
.execute()  
.actionGet();

But I'm getting validation exception :  
java.util.concurrent.ExecutionException:  
org.elasticsearch.action.ActionRequestValidationException: Validation  
Failed: 1: type is missing2: id is missing

Is there way to delete all documents not using 'delete by query API' ?

Thank You and Best Regards.

---

<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:** [June 22, 2011, 7:32pm UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/2 "2011-06-22T19:32:59Z")

</div>

Hi,

I think that prepareDelete() method is only to delete one single document by its Id.  
As you didn't tell ES the type and the Id, you get this ValidationException.

To delete one single document, use :  
DeleteResponse response = client.prepareDelete("twitter", "tweet", "1")  
.execute()  
.actionGet();

If you want to delete a full index, use the HTTP/REST API

curl -XDELETE '[http://localhost:9200/twitter/](http://localhost:9200/twitter/)'

I don't know if you can do it also using the admin client or not.

Hope this helps  
David.

---

<div class="post-metadata">

**Author:** ![Slava\_G](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@Slava\_G](https://discuss.elastic.co/u/Slava_G)\
**Post date:** [June 23, 2011, 4:13am UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/3 "2011-06-23T04:13:14Z")

</div>

Hi,  
I found that via admin interface I can delete index.

But what about deleting index of particular type only, how that could  
be done, without specifying the id of the documents ?

On Jun 22, 12:32 pm, dadoonet [da...@pilato.fr](mailto:da...@pilato.fr) wrote:

> Hi,
> 
> I think that prepareDelete() method is only to delete one single document by  
> its Id.  
> As you didn't tell ES the type and the Id, you get this ValidationException.
> 
> To delete one single document, use :  
> DeleteResponse response = client.prepareDelete("twitter", "tweet", "1")  
> .execute()  
> .actionGet();
> 
> If you want to delete a full index, use the HTTP/REST API
> 
> curl -XDELETE '[http://localhost:9200/twitter/](http://localhost:9200/twitter/)'
> 
> I don't know if you can do it also using the admin client or not.
> 
> Hope this helps  
> David.
> 
> --  
> View this message in context:[http://elasticsearch-users.115913.n3.nabble.com/Java-API-delete-all-d](http://elasticsearch-users.115913.n3.nabble.com/Java-API-delete-all-d)...  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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:** [June 23, 2011, 8:38am UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/4 "2011-06-23T08:38:38Z")

</div>

On Wed, 2011-06-22 at 21:13 -0700, slavag wrote:

> Hi,  
> I found that via admin interface I can delete index.
> 
> But what about deleting index of particular type only, how that could  
> be done, without specifying the id of the documents ?

You can - just delete 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:** [June 23, 2011, 11:57am UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/5 "2011-06-23T11:57:14Z")

</div>

One note, many times it might make sense to actually drop the index and rebuild it.

On Thursday, June 23, 2011 at 11:38 AM, Clinton Gormley wrote:

> On Wed, 2011-06-22 at 21:13 -0700, slavag wrote:
> 
> > Hi,  
> > I found that via admin interface I can delete index.
> > 
> > But what about deleting index of particular type only, how that could  
> > be done, without specifying the id of the documents ?
> 
> You can - just delete the mapping
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-mapping.html)
> 
> clint

---

<div class="post-metadata">

**Author:** ![Slava\_G](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@Slava\_G](https://discuss.elastic.co/u/Slava_G)\
**Post date:** [June 23, 2011, 1:56pm UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/6 "2011-06-23T13:56:53Z")

</div>

Yes, I agree with you, but this can be doe while the original data is  
exist, and if it not - then little bit problematic 🙂  
So, any suggestion how can I delete such index and type ?

On Jun 23, 4:57 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> One note, many times it might make sense to actually drop the index and rebuild it.
> 
> On Thursday, June 23, 2011 at 11:38 AM, Clinton Gormley wrote:
> 
> > On Wed, 2011-06-22 at 21:13 -0700, slavag wrote:
> > 
> > > Hi,  
> > > I found that via admin interface I can delete index.
> 
> > > But what about deleting index of particular type only, how that could  
> > > be done, without specifying the id of the documents ?
> 
> > You can - just delete the mapping
> 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-delete)...
> 
> > clint

---

<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 23, 2011, 2:43pm UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/7 "2011-06-23T14:43:55Z")

</div>

As Clint already mentioned, you can delete mapping for the type and it will  
delete all data from this type as well:

client1.admin().indices().prepareDeleteMapping(indexKey  
).setType(typeKey).execute().actionGet();

or you can execute deleteByQuery with matchAllQuery filtered by the type.

On Thu, Jun 23, 2011 at 9:56 AM, slavag [slavago@gmail.com](mailto:slavago@gmail.com) wrote:

> Yes, I agree with you, but this can be doe while the original data is  
> exist, and if it not - then little bit problematic 🙂  
> So, any suggestion how can I delete such index and type ?
> 
> On Jun 23, 4:57 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > One note, many times it might make sense to actually drop the index and  
> > rebuild it.
> > 
> > On Thursday, June 23, 2011 at 11:38 AM, Clinton Gormley wrote:
> > 
> > > On Wed, 2011-06-22 at 21:13 -0700, slavag wrote:
> > > 
> > > > Hi,  
> > > > I found that via admin interface I can delete index.
> > 
> > > > But what about deleting index of particular type only, how that could  
> > > > be done, without specifying the id of the documents ?
> > 
> > > You can - just delete the mapping
> > 
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-delete).  
> > > ..
> > 
> > > clint

---

<div class="post-metadata">

**Author:** ![Slava\_G](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@Slava\_G](https://discuss.elastic.co/u/Slava_G)\
**Post date:** [June 23, 2011, 4:43pm UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/8 "2011-06-23T16:43:41Z")

</div>

Cool, Thank You for answer,  
Question about delete mapping - if there another index with the same  
type will it be also deleted ?

On Jun 23, 7:43 am, Igor Motov [i...@motovs.org](mailto:i...@motovs.org) wrote:

> As Clint already mentioned, you can delete mapping for the type and it will  
> delete all data from this type as well:
> 
> client1.admin().indices().prepareDeleteMapping(indexKey  
> ).setType(typeKey).execute().actionGet();
> 
> or you can execute deleteByQuery with matchAllQuery filtered by the type.
> 
> On Thu, Jun 23, 2011 at 9:56 AM, slavag [slav...@gmail.com](mailto:slav...@gmail.com) wrote:
> 
> > Yes, I agree with you, but this can be doe while the original data is  
> > exist, and if it not - then little bit problematic 🙂  
> > So, any suggestion how can I delete such index and type ?
> 
> > On Jun 23, 4:57 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > 
> > > One note, many times it might make sense to actually drop the index and  
> > > rebuild it.
> 
> > > On Thursday, June 23, 2011 at 11:38 AM, Clinton Gormley wrote:
> > > 
> > > > On Wed, 2011-06-22 at 21:13 -0700, slavag wrote:
> > > > 
> > > > > Hi,  
> > > > > I found that via admin interface I can delete index.
> 
> > > > > But what about deleting index of particular type only, how that could  
> > > > > be done, without specifying the id of the documents ?
> 
> > > > You can - just delete the mapping
> 
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-delete).  
> > > > ..
> 
> > > > clint

---

<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 23, 2011, 4:54pm UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/9 "2011-06-23T16:54:03Z")

</div>

No, if you specify the index name (parameter indexKey) in  
prepareDeleteMapping, it will delete the type only from the index specified  
in indexKey.

If you want to delete this type from all indices in the cluster, don't  
specify indexKey:

client1.admin().indices().prepareDeleteMapping().setType(typeKey).execute().actionGet();

On Thu, Jun 23, 2011 at 12:43 PM, slavag [slavago@gmail.com](mailto:slavago@gmail.com) wrote:

> Cool, Thank You for answer,  
> Question about delete mapping - if there another index with the same  
> type will it be also deleted ?
> 
> On Jun 23, 7:43 am, Igor Motov [i...@motovs.org](mailto:i...@motovs.org) wrote:
> 
> > As Clint already mentioned, you can delete mapping for the type and it  
> > will  
> > delete all data from this type as well:
> > 
> > client1.admin().indices().prepareDeleteMapping(indexKey  
> > ).setType(typeKey).execute().actionGet();
> > 
> > or you can execute deleteByQuery with matchAllQuery filtered by the type.
> > 
> > On Thu, Jun 23, 2011 at 9:56 AM, slavag [slav...@gmail.com](mailto:slav...@gmail.com) wrote:
> > 
> > > Yes, I agree with you, but this can be doe while the original data is  
> > > exist, and if it not - then little bit problematic 🙂  
> > > So, any suggestion how can I delete such index and type ?
> > 
> > > On Jun 23, 4:57 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > > 
> > > > One note, many times it might make sense to actually drop the index  
> > > > and  
> > > > rebuild it.
> > 
> > > > On Thursday, June 23, 2011 at 11:38 AM, Clinton Gormley wrote:
> > > > 
> > > > > On Wed, 2011-06-22 at 21:13 -0700, slavag wrote:
> > > > > 
> > > > > > Hi,  
> > > > > > I found that via admin interface I can delete index.
> > 
> > > > > > But what about deleting index of particular type only, how that  
> > > > > > could  
> > > > > > be done, without specifying the id of the documents ?
> > 
> > > > > You can - just delete the mapping
> > 
> > > > >
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-delete).
> 
> > > ..
> > 
> > > > > clint

---

<div class="post-metadata">

**Author:** ![Slava\_G](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@Slava\_G](https://discuss.elastic.co/u/Slava_G)\
**Post date:** [June 23, 2011, 10:26pm UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/10 "2011-06-23T22:26:26Z")

</div>

Thank You !!!!

On Jun 23, 9:54 am, Igor Motov [i...@motovs.org](mailto:i...@motovs.org) wrote:

> No, if you specify the index name (parameter indexKey) in  
> prepareDeleteMapping, it will delete the type only from the index specified  
> in indexKey.
> 
> If you want to delete this type from all indices in the cluster, don't  
> specify indexKey:
> 
> client1.admin().indices().prepareDeleteMapping().setType(typeKey).execute() .actionGet();
> 
> On Thu, Jun 23, 2011 at 12:43 PM, slavag [slav...@gmail.com](mailto:slav...@gmail.com) wrote:
> 
> > Cool, Thank You for answer,  
> > Question about delete mapping - if there another index with the same  
> > type will it be also deleted ?
> 
> > On Jun 23, 7:43 am, Igor Motov [i...@motovs.org](mailto:i...@motovs.org) wrote:
> > 
> > > As Clint already mentioned, you can delete mapping for the type and it  
> > > will  
> > > delete all data from this type as well:
> 
> > > client1.admin().indices().prepareDeleteMapping(indexKey  
> > > ).setType(typeKey).execute().actionGet();
> 
> > > or you can execute deleteByQuery with matchAllQuery filtered by the type.
> 
> > > On Thu, Jun 23, 2011 at 9:56 AM, slavag [slav...@gmail.com](mailto:slav...@gmail.com) wrote:
> > > 
> > > > Yes, I agree with you, but this can be doe while the original data is  
> > > > exist, and if it not - then little bit problematic 🙂  
> > > > So, any suggestion how can I delete such index and type ?
> 
> > > > On Jun 23, 4:57 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > > > 
> > > > > One note, many times it might make sense to actually drop the index  
> > > > > and  
> > > > > rebuild it.
> 
> > > > > On Thursday, June 23, 2011 at 11:38 AM, Clinton Gormley wrote:
> > > > > 
> > > > > > On Wed, 2011-06-22 at 21:13 -0700, slavag wrote:
> > > > > > 
> > > > > > > Hi,  
> > > > > > > I found that via admin interface I can delete index.
> 
> > > > > > > But what about deleting index of particular type only, how that  
> > > > > > > could  
> > > > > > > be done, without specifying the id of the documents ?
> 
> > > > > > You can - just delete the mapping
> 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-delete).
> > 
> > > > ..
> 
> > > > > > clint

---

<div class="post-metadata">

**Author:** ![voootla561](https://avatars.discourse-cdn.com/v4/letter/v/e0b2c6/32.png) [@voootla561](https://discuss.elastic.co/u/voootla561)\
**Post date:** [June 30, 2017, 8:14am UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/11 "2017-06-30T08:14:11Z")

</div>

in latest release's of ES we can't do ([https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-delete-mapping.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-delete-mapping.html) )  
client.admin().indices().prepareDeleteMapping(indexKey  
).setType(indexType).execute().actionGet();

because there is no method 'prepareDeleteMapping'

can any one suggest how to delete particular type from index ?

---

<div class="post-metadata">

**Author:** ![Pramod\_Jundre](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@Pramod\_Jundre](https://discuss.elastic.co/u/Pramod_Jundre)\
**Post date:** [December 7, 2017, 9:39am UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/12 "2017-12-07T09:39:56Z")

</div>

From elastic 5.X you can do it like this:

DeleteByQueryRequestBuilder dbqb= DeleteByQueryAction.INSTANCE  
.newRequestBuilder(RTPCRFElasticSearch.getClient());  
dbqb.source().setIndices("index")  
.setTypes("type");  
BulkIndexByScrollResponse resp = dbqb  
.filter(QueryBuilders.matchAllQuery()).get();

---

<div class="post-metadata">

**Author:** ![agorodetskaya](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@agorodetskaya](https://discuss.elastic.co/u/agorodetskaya)\
**Post date:** [March 14, 2018, 4:20pm UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/13 "2018-03-14T16:20:10Z")

</div>

Thanks a lot!

---

<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:** [November 4, 2022, 4:01am UTC](https://discuss.elastic.co/t/java-api-delete-all-documents-within-the-specific-index-and-the-specific-type/4681/14 "2022-11-04T04:01:24Z")

</div>


