Skip to content

Commit e2d1c04

Browse files
committed
feat(dart): deleteMultipleIndices
1 parent 387310d commit e2d1c04

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{{> snippets/import}}
2+
3+
void deleteMultipleIndices() async {
4+
// You need an API key with `deleteIndex`
5+
{{> snippets/init}}
6+
7+
// List all indices
8+
var indices = await {{#dynamicSnippet}}listIndicesSimple{{/dynamicSnippet}};
9+
10+
// Primary indices don't have a `primary` key
11+
var primaryIndices = indices.items.where((element) => element.primary == null).toList();
12+
var replicaIndices = indices.items.where((element) => element.primary != null).toList();
13+
14+
// Delete primary indices first
15+
if (primaryIndices.isNotEmpty) {
16+
List<MultipleBatchRequest> requests = primaryIndices.map((element) =>
17+
MultipleBatchRequest(
18+
action: Action.delete,
19+
indexName: element.name
20+
)
21+
).toList();
22+
await {{#dynamicSnippet}}deleteMultipleIndicesPrimary{{/dynamicSnippet}};
23+
print("Deleted primary indices.");
24+
}
25+
26+
// Now, delete replica indices
27+
if (replicaIndices.isNotEmpty) {
28+
List<MultipleBatchRequest> requests = replicaIndices.map((element) =>
29+
MultipleBatchRequest(
30+
action: Action.delete,
31+
indexName: element.name
32+
)
33+
).toList();
34+
await {{#dynamicSnippet}}deleteMultipleIndicesReplica{{/dynamicSnippet}};
35+
print("Deleted replica indices.");
36+
}
37+
}

0 commit comments

Comments
 (0)