Delete Index API

edit

Delete Index Request

edit

A DeleteIndexRequest requires an index argument:

DeleteIndexRequest request = new DeleteIndexRequest("posts"); 

Index

Optional arguments

edit

The following arguments can optionally be provided:

request.timeout(TimeValue.timeValueMinutes(2)); 
request.timeout("2m"); 

Timeout to wait for the all the nodes to acknowledge the index deletion as a TimeValue

Timeout to wait for the all the nodes to acknowledge the index deletion as a String

request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); 
request.masterNodeTimeout("1m"); 

Timeout to connect to the master node as a TimeValue

Timeout to connect to the master node as a String

request.indicesOptions(IndicesOptions.lenientExpandOpen()); 

Setting IndicesOptions controls how unavailable indices are resolved and how wildcard expressions are expanded

Synchronous Execution

edit
DeleteIndexResponse deleteIndexResponse = client.indices().deleteIndex(request);

Asynchronous Execution

edit
client.indices().deleteIndexAsync(request, new ActionListener<DeleteIndexResponse>() {
    @Override
    public void onResponse(DeleteIndexResponse deleteIndexResponse) {
        
    }

    @Override
    public void onFailure(Exception e) {
        
    }
});

Called when the execution is successfully completed. The response is provided as an argument

Called in case of failure. The raised exception is provided as an argument

Delete Index Response

edit

The returned DeleteIndexResponse allows to retrieve information about the executed operation as follows:

boolean acknowledged = deleteIndexResponse.isAcknowledged(); 

Indicates whether all of the nodes have acknowledged the request or not

If the index was not found, an ElasticsearchException will be thrown:

try {
    DeleteIndexRequest request = new DeleteIndexRequest("does_not_exist");
    client.indices().deleteIndex(request);
} catch (ElasticsearchException exception) {
    if (exception.status() == RestStatus.NOT_FOUND) {
        
    }
}

Do something if the index to be deleted was not found