Skip to content

Commit bd3f490

Browse files
committed
Delete IndexAPI: Allow to delete more than one index or _all indices, closes elastic#791.
1 parent 09fbc91 commit bd3f490

File tree

6 files changed

+57
-28
lines changed

6 files changed

+57
-28
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/delete/DeleteIndexRequest.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838
public class DeleteIndexRequest extends MasterNodeOperationRequest {
3939

40-
private String index;
40+
private String[] indices;
4141

4242
private TimeValue timeout = timeValueSeconds(10);
4343

@@ -48,22 +48,31 @@ public class DeleteIndexRequest extends MasterNodeOperationRequest {
4848
* Constructs a new delete index request for the specified index.
4949
*/
5050
public DeleteIndexRequest(String index) {
51-
this.index = index;
51+
this.indices = new String[]{index};
52+
}
53+
54+
public DeleteIndexRequest(String... indices) {
55+
this.indices = indices;
5256
}
5357

5458
@Override public ActionRequestValidationException validate() {
5559
ActionRequestValidationException validationException = null;
56-
if (index == null) {
57-
validationException = addValidationError("index is missing", validationException);
60+
if (indices == null) {
61+
validationException = addValidationError("index / indices is missing", validationException);
5862
}
5963
return validationException;
6064
}
6165

66+
public DeleteIndexRequest indices(String... indices) {
67+
this.indices = indices;
68+
return this;
69+
}
70+
6271
/**
6372
* The index to delete.
6473
*/
65-
String index() {
66-
return index;
74+
String[] indices() {
75+
return indices;
6776
}
6877

6978
/**
@@ -93,13 +102,23 @@ public DeleteIndexRequest timeout(String timeout) {
93102

94103
@Override public void readFrom(StreamInput in) throws IOException {
95104
super.readFrom(in);
96-
index = in.readUTF();
105+
indices = new String[in.readVInt()];
106+
for (int i = 0; i < indices.length; i++) {
107+
indices[i] = in.readUTF();
108+
}
97109
timeout = readTimeValue(in);
98110
}
99111

100112
@Override public void writeTo(StreamOutput out) throws IOException {
101113
super.writeTo(out);
102-
out.writeUTF(index);
114+
if (indices == null) {
115+
out.writeVInt(0);
116+
} else {
117+
out.writeVInt(indices.length);
118+
for (String index : indices) {
119+
out.writeUTF(index);
120+
}
121+
}
103122
timeout.writeTo(out);
104123
}
105124
}

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/delete/TransportDeleteIndexAction.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.action.admin.indices.delete;
2121

2222
import org.elasticsearch.ElasticSearchException;
23+
import org.elasticsearch.action.ActionListener;
2324
import org.elasticsearch.action.TransportActions;
2425
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
2526
import org.elasticsearch.cluster.ClusterService;
@@ -66,25 +67,32 @@ public class TransportDeleteIndexAction extends TransportMasterNodeOperationActi
6667
return new DeleteIndexResponse();
6768
}
6869

70+
@Override protected void doExecute(DeleteIndexRequest request, ActionListener<DeleteIndexResponse> listener) {
71+
request.indices(clusterService.state().metaData().concreteIndices(request.indices()));
72+
super.doExecute(request, listener);
73+
}
74+
6975
@Override protected ClusterBlockException checkBlock(DeleteIndexRequest request, ClusterState state) {
70-
return state.blocks().indexBlockedException(ClusterBlockLevel.METADATA, request.index());
76+
return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA, request.indices());
7177
}
7278

7379
@Override protected DeleteIndexResponse masterOperation(DeleteIndexRequest request, ClusterState state) throws ElasticSearchException {
7480
final AtomicReference<DeleteIndexResponse> responseRef = new AtomicReference<DeleteIndexResponse>();
7581
final AtomicReference<Throwable> failureRef = new AtomicReference<Throwable>();
76-
final CountDownLatch latch = new CountDownLatch(1);
77-
deleteIndexService.deleteIndex(new MetaDataDeleteIndexService.Request(request.index()).timeout(request.timeout()), new MetaDataDeleteIndexService.Listener() {
78-
@Override public void onResponse(MetaDataDeleteIndexService.Response response) {
79-
responseRef.set(new DeleteIndexResponse(response.acknowledged()));
80-
latch.countDown();
81-
}
82-
83-
@Override public void onFailure(Throwable t) {
84-
failureRef.set(t);
85-
latch.countDown();
86-
}
87-
});
82+
final CountDownLatch latch = new CountDownLatch(request.indices().length);
83+
for (String index : request.indices()) {
84+
deleteIndexService.deleteIndex(new MetaDataDeleteIndexService.Request(index).timeout(request.timeout()), new MetaDataDeleteIndexService.Listener() {
85+
@Override public void onResponse(MetaDataDeleteIndexService.Response response) {
86+
responseRef.set(new DeleteIndexResponse(response.acknowledged()));
87+
latch.countDown();
88+
}
89+
90+
@Override public void onFailure(Throwable t) {
91+
failureRef.set(t);
92+
latch.countDown();
93+
}
94+
});
95+
}
8896

8997
try {
9098
latch.await();

modules/elasticsearch/src/main/java/org/elasticsearch/client/IndicesAdminClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ public interface IndicesAdminClient {
150150
/**
151151
* Deletes an index based on the index name.
152152
*
153-
* @param index The index name to delete
153+
* @param indices The indices to delete. Empty array to delete all indices.
154154
*/
155-
DeleteIndexRequestBuilder prepareDelete(String index);
155+
DeleteIndexRequestBuilder prepareDelete(String... indices);
156156

157157
/**
158158
* Closes an index based on the index name.

modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/delete/DeleteIndexRequestBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
*/
3232
public class DeleteIndexRequestBuilder extends BaseIndicesRequestBuilder<DeleteIndexRequest, DeleteIndexResponse> {
3333

34-
public DeleteIndexRequestBuilder(IndicesAdminClient indicesClient, String index) {
35-
super(indicesClient, new DeleteIndexRequest(index));
34+
public DeleteIndexRequestBuilder(IndicesAdminClient indicesClient, String... indices) {
35+
super(indicesClient, new DeleteIndexRequest(indices));
3636
}
3737

3838
/**

modules/elasticsearch/src/main/java/org/elasticsearch/client/support/AbstractIndicesAdminClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public abstract class AbstractIndicesAdminClient implements InternalIndicesAdmin
5555
return new CreateIndexRequestBuilder(this, index);
5656
}
5757

58-
@Override public DeleteIndexRequestBuilder prepareDelete(String index) {
59-
return new DeleteIndexRequestBuilder(this, index);
58+
@Override public DeleteIndexRequestBuilder prepareDelete(String... indices) {
59+
return new DeleteIndexRequestBuilder(this, indices);
6060
}
6161

6262
@Override public CloseIndexRequestBuilder prepareClose(String index) {

modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/delete/RestDeleteIndexAction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import static org.elasticsearch.common.unit.TimeValue.*;
3636
import static org.elasticsearch.rest.RestStatus.*;
37+
import static org.elasticsearch.rest.action.support.RestActions.*;
3738

3839
/**
3940
* @author kimchy (shay.banon)
@@ -42,11 +43,12 @@ public class RestDeleteIndexAction extends BaseRestHandler {
4243

4344
@Inject public RestDeleteIndexAction(Settings settings, Client client, RestController controller) {
4445
super(settings, client);
46+
controller.registerHandler(RestRequest.Method.DELETE, "/", this);
4547
controller.registerHandler(RestRequest.Method.DELETE, "/{index}", this);
4648
}
4749

4850
@Override public void handleRequest(final RestRequest request, final RestChannel channel) {
49-
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(request.param("index"));
51+
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(splitIndices(request.param("index")));
5052
deleteIndexRequest.timeout(request.paramAsTime("timeout", timeValueSeconds(10)));
5153
client.admin().indices().delete(deleteIndexRequest, new ActionListener<DeleteIndexResponse>() {
5254
@Override public void onResponse(DeleteIndexResponse response) {

0 commit comments

Comments
 (0)