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().delete(request);

Asynchronous Execution

edit

The asynchronous execution of a delete index request requires both the DeleteIndexRequest instance and an ActionListener instance to be passed to the asynchronous method:

client.indices().deleteAsync(request, listener); 

The DeleteIndexRequest to execute and the ActionListener to use when the execution completes

The asynchronous method does not block and returns immediately. Once it is completed the ActionListener is called back using the onResponse method if the execution successfully completed or using the onFailure method if it failed.

A typical listener for DeleteIndexResponse looks like:

ActionListener<DeleteIndexResponse> listener = 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

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

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

Do something if the index to be deleted was not found