xkcoding/spring-boot-demo · error · ElasticsearchException

删除索引 {index} 失败

Error message

删除索引 {index} 失败

What it means

Thrown by deleteIndexRequest when client.indices().delete() raises an IOException during the HTTP call to delete an index from the Elasticsearch cluster. As with createIndexRequest, only IOException is caught; ES-level errors (index not found) surface as RuntimeExceptions and bypass this handler. The original cause is also discarded.

Source

Thrown at demo-elasticsearch-rest-high-level-client/src/main/java/com/xkcoding/elasticsearch/service/base/BaseElasticsearchService.java:84

            log.info(" whether all of the nodes have acknowledged the request : {}", createIndexResponse.isAcknowledged());
            log.info(" Indicates whether the requisite number of shard copies were started for each shard in the index before timing out :{}", createIndexResponse.isShardsAcknowledged());
        } catch (IOException e) {
            throw new ElasticsearchException("创建索引 {" + index + "} 失败");
        }
    }

    /**
     * delete elasticsearch index
     *
     * @param index elasticsearch index name
     * @author fxbin
     */
    protected void deleteIndexRequest(String index) {
        DeleteIndexRequest deleteIndexRequest = buildDeleteIndexRequest(index);
        try {
            client.indices().delete(deleteIndexRequest, COMMON_OPTIONS);
        } catch (IOException e) {
            throw new ElasticsearchException("删除索引 {" + index + "} 失败");
        }
    }

    /**
     * build DeleteIndexRequest
     *
     * @param index elasticsearch index name
     * @author fxbin
     */
    private static DeleteIndexRequest buildDeleteIndexRequest(String index) {
        return new DeleteIndexRequest(index);
    }

    /**
     * build IndexRequest
     *
     * @param index  elasticsearch index name
     * @param id     request object id

View on GitHub (pinned to 87a142f960)

Solutions

  1. Confirm the ES cluster is running and reachable from the application host.
  2. Check that the index name passed is valid and exists before calling delete (use IndicesClient.exists() first).
  3. Pass the IOException as cause: new ElasticsearchException("...", e) to preserve diagnostics.
  4. Add a catch for ElasticsearchStatusException to handle the 'index not found' case without crashing.

Example fix

// before
} catch (IOException e) {
    throw new ElasticsearchException("删除索引 {" + index + "} 失败");
}

// after
} catch (IOException e) {
    throw new ElasticsearchException("删除索引 {" + index + "} 失败", e);
}
Defensive patterns

Strategy: validation

Validate before calling

// Check index existence before deleting
try {
    org.elasticsearch.client.indices.GetIndexRequest existsReq =
        new org.elasticsearch.client.indices.GetIndexRequest(index);
    if (!client.indices().exists(existsReq, COMMON_OPTIONS)) {
        log.warn("Index {} does not exist, skipping delete", index);
        return;
    }
} catch (IOException e) {
    log.error("Cannot verify index existence", e);
}

Try / catch

try {
    deleteIndexRequest(index);
} catch (ElasticsearchException e) {
    log.error("Index deletion failed for {}: {}", index, e.getMessage());
    // Idempotent: if index not found, treat as success
    if (e.getMessage() != null && e.getMessage().contains("not found")) return;
    throw e;
}

Prevention

When it happens

Trigger: Calling deleteIndexRequest on an index that does not exist while the cluster is reachable (produces a RuntimeException, not IOException — will not be caught here). IOException variant triggers on transport failures: cluster down, network timeout, or connection refused.

Common situations: Cluster offline during cleanup/shutdown; network instability between app and ES nodes; attempting to delete an already-deleted index during test teardown; wrong clusterNodes configuration.

Related errors


AI-assisted analysis of xkcoding/spring-boot-demo@87a142f960 (2026-08-14). Data as JSON: /api/errors/bd03b653300e37a1. Report an issue: GitHub.