diff --git a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyAction.java b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyAction.java index c0dc07a3382ba..bf17ffaf6b956 100644 --- a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyAction.java +++ b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyAction.java @@ -9,6 +9,7 @@ import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.admin.indices.get.GetIndexRequest; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.AcknowledgedResponse; @@ -126,7 +127,12 @@ protected void masterOperation( return; } - deleteIndicesAndPolicy(request.getName(), ActionListener.wrap((response) -> { + GetIndexRequest indices = new GetIndexRequest().indices(EnrichPolicy.getBaseName(request.getName()) + "-*") + .indicesOptions(IndicesOptions.lenientExpand()); + + String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, indices); + + deleteIndicesAndPolicy(concreteIndices, request.getName(), ActionListener.wrap((response) -> { enrichPolicyLocks.releasePolicy(request.getName()); listener.onResponse(response); }, (exc) -> { @@ -135,10 +141,15 @@ protected void masterOperation( })); } - private void deleteIndicesAndPolicy(String name, ActionListener listener) { - // delete all enrich indices for this policy - DeleteIndexRequest deleteRequest = new DeleteIndexRequest().indices(EnrichPolicy.getBaseName(name) + "-*") - .indicesOptions(LENIENT_OPTIONS); + private void deleteIndicesAndPolicy(String[] indices, String name, ActionListener listener) { + if (indices.length == 0) { + deletePolicy(name, listener); + return; + } + + // delete all enrich indices for this policy, we delete concrete indices here but not a wildcard index expression + // as the setting 'action.destructive_requires_name' may be set to true + DeleteIndexRequest deleteRequest = new DeleteIndexRequest().indices(indices).indicesOptions(LENIENT_OPTIONS); client.admin().indices().delete(deleteRequest, ActionListener.wrap((response) -> { if (response.isAcknowledged() == false) { diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyActionTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyActionTests.java index efdde89d3f310..911c41835d425 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyActionTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyActionTests.java @@ -8,8 +8,11 @@ import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.support.ActionTestUtils; +import org.elasticsearch.action.support.DestructiveOperations; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.IndexNotFoundException; @@ -23,6 +26,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicReference; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.randomEnrichPolicy; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.nullValue; @@ -115,6 +119,14 @@ public void testDeleteIsNotLocked() throws Exception { AtomicReference error = saveEnrichPolicy(name, policy, clusterService); assertThat(error.get(), nullValue()); + boolean destructiveRequiresName = randomBoolean(); + if (destructiveRequiresName) { + Settings settings = Settings.builder() + .put(DestructiveOperations.REQUIRES_NAME_SETTING.getKey(), destructiveRequiresName) + .build(); + assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings)); + } + createIndex(EnrichPolicy.getBaseName(name) + "-foo1"); createIndex(EnrichPolicy.getBaseName(name) + "-foo2"); @@ -151,6 +163,11 @@ public void onFailure(final Exception e) { .get() ); + if (destructiveRequiresName) { + Settings settings = Settings.builder().putNull(DestructiveOperations.REQUIRES_NAME_SETTING.getKey()).build(); + assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings)); + } + EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode(EnrichPolicyLocks.class); assertFalse(enrichPolicyLocks.captureExecutionState().isAnyPolicyInFlight());