core/src/main/java/org/elasticsearch/indices/IndicesService.java
public void deleteShardStore(String reason, ShardId shardId, ClusterState clusterState) throws IOException {
        final IndexMetaData metaData = clusterState.getMetaData().indices().get(shardId.getIndexName());

        final IndexSettings indexSettings = buildIndexSettings(metaData);
        if (canDeleteShardContent(shardId, indexSettings) == false) {
            throw new IllegalStateException("Can't delete shard " + shardId);
        }
        nodeEnv.deleteShardDirectorySafe(shardId, indexSettings);
        logger.debug("{} deleted shard reason [{}]", shardId, reason);

        if (clusterState.nodes().localNode().isMasterNode() == false && // master nodes keep the index meta data, even if having no shards..
                canDeleteIndexContents(shardId.getIndex(), indexSettings, false)) {
            if (nodeEnv.findAllShardIds(shardId.getIndex()).isEmpty()) {
                try {
                    // note that deleteIndexStore have more safety checks and may throw an exception if index was concurrently created.
                    deleteIndexStore("no longer used", metaData, clusterState, false);
                } catch (Exception e) {
                    // wrap the exception to indicate we already deleted the shard
                    throw new ElasticsearchException("failed to delete unused index after deleting its last shard (" + shardId + ")", e);
                }
            } else {
                logger.trace("[{}] still has shard stores, leaving as is", shardId.getIndex());
            }
        }
    }
/**
     * This method deletes the shard contents on disk for the given shard ID. This method will fail if the shard deleting
     * is prevented by {@link #canDeleteShardContent(ShardId, IndexSettings)}
     * of if the shards lock can not be acquired.
     *
     * On data nodes, if the deleted shard is the last shard folder in its index, the method will attempt to remove the index folder as well.
     *
     * @param reason the reason for the shard deletion
     * @param shardId the shards ID to delete
     * @param clusterState . This is required to access the indexes settings etc.
     * @throws IOException if an IOException occurs
     */
This method deletes the shard contents on disk for the given shard ID. This method will fail if the shard deleting
is prevented by  #canDeleteShardContent(ShardId, IndexSettings)
of if the shards lock can not be acquired.

On data nodes, if the deleted shard is the last shard folder in its index, the method will attempt to remove the index folder as well.

@param reason the reason for the shard deletion
@param shardId the shards ID to delete
@param clusterState . This is required to access the indexes settings etc.
This method deletes the shard contents on disk for the given shard ID . This method will fail if the shard deleting is prevented by #canDeleteShardContent ( ShardId IndexSettings ) of if the shards lock can not be acquired . On data nodes if the deleted shard is the last shard folder in its index the method will attempt to remove the index folder as well . @param reason the reason for the shard deletion @param shardId the shards ID to delete @param clusterState . This is required to access the indexes settings etc .
public void deleteShardStore(String reason, ShardId shardId, ClusterState clusterState) throws IOException {
        final IndexMetaData metaData = clusterState.getMetaData().indices().get(shardId.getIndexName());
        final IndexSettings indexSettings = buildIndexSettings(metaData);
        if (canDeleteShardContent(shardId, indexSettings) == false) {
            throw new IllegalStateException("Can't delete shard " + shardId);
        }
        nodeEnv.deleteShardDirectorySafe(shardId, indexSettings);
        logger.debug("{} deleted shard reason [{}]", shardId, reason);
        if (clusterState.nodes().localNode().isMasterNode() == false && 
                canDeleteIndexContents(shardId.getIndex(), indexSettings, false)) {
            if (nodeEnv.findAllShardIds(shardId.getIndex()).isEmpty()) {
                try {
                    deleteIndexStore("no longer used", metaData, clusterState, false);
                } catch (Exception e) {
                    throw new ElasticsearchException("failed to delete unused index after deleting its last shard (" + shardId + ")", e);
                }
            } else {
                logger.trace("[{}] still has shard stores, leaving as is", shardId.getIndex());
            }
        }
    }
public void deleteShardStore ( String reason , ShardId shardId , ClusterState clusterState ) throws IOException { final IndexMetaData metaData = clusterState . getMetaData ( ) . indices ( ) . get ( shardId . getIndexName ( ) ) ; final IndexSettings indexSettings = buildIndexSettings ( metaData ) ; if ( canDeleteShardContent ( shardId , indexSettings ) == false ) { throw new IllegalStateException ( "Can't delete shard " + shardId ) ; } nodeEnv . deleteShardDirectorySafe ( shardId , indexSettings ) ; logger . debug ( "{} deleted shard reason [{}]" , shardId , reason ) ; if ( clusterState . nodes ( ) . localNode ( ) . isMasterNode ( ) == false && canDeleteIndexContents ( shardId . getIndex ( ) , indexSettings , false ) ) { if ( nodeEnv . findAllShardIds ( shardId . getIndex ( ) ) . isEmpty ( ) ) { try { deleteIndexStore ( "no longer used" , metaData , clusterState , false ) ; } catch ( Exception e ) { throw new ElasticsearchException ( "failed to delete unused index after deleting its last shard (" + shardId + ")" , e ) ; } } else { logger . trace ( "[{}] still has shard stores, leaving as is" , shardId . getIndex ( ) ) ; } } }
This method deletes the shard contents on disk for the given shard ID.
This method deletes the shard contents on disk for the given shard ID .
// master nodes keep the index meta data, even if having no shards.. // note that deleteIndexStore have more safety checks and may throw an exception if index was concurrently created. // wrap the exception to indicate we already deleted the shard