performanceelasticsearchelasticsearch-indices

How does the 'delete index' command work inside ES?


How does the 'delete index' command work inside ES?

Are there any risks when using the 'delete index' command on a running ES cluster? will this command cost too much CPU or memory?


Solution

  • Deleting the indices are normally pretty fast and Elasticsearch doesn't actually delete all the documents when it sends the success response of a delete indices request.

    Elasticsearch mainly updates the cluster state(maintained on all the nodes of a cluster) to mark the indices as deleted and major heavy-lifting is done in updating it as well as some other things like routing table, metadata, etc.

    this is the main method in Elaticsearch source code, which would help you understand the thing which I mentioned above and internals of the delete index.

    Some important code snippet from above link

    RoutingTable.Builder routingTableBuilder = RoutingTable.builder(currentState.routingTable());
            Metadata.Builder metadataBuilder = Metadata.builder(meta);
            ClusterBlocks.Builder clusterBlocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
    
            final IndexGraveyard.Builder graveyardBuilder = IndexGraveyard.builder(metadataBuilder.indexGraveyard());
            final int previousGraveyardSize = graveyardBuilder.tombstones().size();
            for (final Index index : indices) {
                String indexName = index.getName();
                logger.info("{} deleting index", index);
                routingTableBuilder.remove(indexName);
                clusterBlocksBuilder.removeIndexBlocks(indexName);
                metadataBuilder.remove(indexName);
            }
            // add tombstones to the cluster state for each deleted index
            final IndexGraveyard currentGraveyard = graveyardBuilder.addTombstones(indices).build(settings);
            metadataBuilder.indexGraveyard(currentGraveyard); // the new graveyard set on the metadata
            logger.trace("{} tombstones purged from the cluster state. Previous tombstone size: {}. Current tombstone size: {}.",
                graveyardBuilder.getNumPurged(), previousGraveyardSize, currentGraveyard.getTombstones().size());
    

    Coming to your question, Are there any risks when using the 'delete index' command on a running ES cluster? will this command cost too much CPU or memory?

    No there is no risk of using delete index request on a running Elasticsearch cluster unless you have a huge cluster state and as mentioned actually deleting the indices happens async and this just updates various state and flags, it doesn't command too much CPU.

    You can also enable the trace log in org.elasticsearch.cluster.metadata.MetadataDeleteIndexService and see how big is your cluster size as its logged in the code snippet above.