Clear Cache APIedit

Clear Cache Requestedit

A ClearIndicesCacheRquest can be applied to one or more indices, or even on _all the indices:

ClearIndicesCacheRequest request = new ClearIndicesCacheRequest("index1"); 
ClearIndicesCacheRequest requestMultiple = new ClearIndicesCacheRequest("index1", "index2"); 
ClearIndicesCacheRequest requestAll = new ClearIndicesCacheRequest(); 

Clears the cache of one index

Clears the cache of multiple indices

Clears the cache of all the indices

Optional argumentsedit


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


Set the query flag to true


Set the fielddata flag to true


Set the request flag to true

request.fields("field1", "field2", "field3"); 

Set the fields parameter

Synchronous Executionedit

ClearIndicesCacheResponse clearCacheResponse = client.indices().clearCache(request, RequestOptions.DEFAULT);

Asynchronous Executionedit

The asynchronous execution of a clear cache request requires both the ClearIndicesCacheRequest instance and an ActionListener instance to be passed to the asynchronous method:

client.indices().clearCacheAsync(request, RequestOptions.DEFAULT, listener); 

The ClearIndicesCacheRequest 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 ClearIndicesCacheResponse looks like:

ActionListener<ClearIndicesCacheResponse> listener = new ActionListener<ClearIndicesCacheResponse>() {
    public void onResponse(ClearIndicesCacheResponse clearCacheResponse) {

    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

Clear Cache Responseedit

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

int totalShards = clearCacheResponse.getTotalShards(); 
int successfulShards = clearCacheResponse.getSuccessfulShards(); 
int failedShards = clearCacheResponse.getFailedShards(); 
DefaultShardOperationFailedException[] failures = clearCacheResponse.getShardFailures(); 

Total number of shards hit by the clear cache request

Number of shards where the clear cache has succeeded

Number of shards where the clear cache has failed

A list of failures if the operation failed on one or more shards

By default, if the indices were not found, an ElasticsearchException will be thrown:

try {
    ClearIndicesCacheRequest request = new ClearIndicesCacheRequest("does_not_exist");
    client.indices().clearCache(request, RequestOptions.DEFAULT);
} catch (ElasticsearchException exception) {
    if (exception.status() == RestStatus.NOT_FOUND) {

Do something if the indices to be cleared were not found