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

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.

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/'

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

Hope this helps
David.

1 Like

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 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/'

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...
Sent from the Elasticsearch Users mailing list archive at Nabble.com.

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

clint

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

clint

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

On Jun 23, 4:57 am, Shay Banon 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...

clint

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 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 :slight_smile:
So, any suggestion how can I delete such index and type ?

On Jun 23, 4:57 am, Shay Banon 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.
..

clint

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 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 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 :slight_smile:
So, any suggestion how can I delete such index and type ?

On Jun 23, 4:57 am, Shay Banon 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.
..

clint

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 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 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 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 :slight_smile:
So, any suggestion how can I delete such index and type ?

On Jun 23, 4:57 am, Shay Banon 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.

..

clint

Thank You !!!!

On Jun 23, 9:54 am, Igor Motov 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 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 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 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 :slight_smile:
So, any suggestion how can I delete such index and type ?

On Jun 23, 4:57 am, Shay Banon 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.

..

clint

in latest release's of ES we can't do (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 ?

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();

2 Likes

Thanks a lot!