Skip to content

Commit

Permalink
feat(dart): deleteMultipleIndices
Browse files Browse the repository at this point in the history
  • Loading branch information
Fluf22 committed Jan 24, 2025
1 parent 387310d commit e2d1c04
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions templates/dart/guides/search/deleteMultipleIndices.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{> snippets/import}}

void deleteMultipleIndices() async {
// You need an API key with `deleteIndex`
{{> snippets/init}}

// List all indices
var indices = await {{#dynamicSnippet}}listIndicesSimple{{/dynamicSnippet}};

// Primary indices don't have a `primary` key
var primaryIndices = indices.items.where((element) => element.primary == null).toList();
var replicaIndices = indices.items.where((element) => element.primary != null).toList();

// Delete primary indices first
if (primaryIndices.isNotEmpty) {
List<MultipleBatchRequest> requests = primaryIndices.map((element) =>
MultipleBatchRequest(
action: Action.delete,
indexName: element.name
)
).toList();
await {{#dynamicSnippet}}deleteMultipleIndicesPrimary{{/dynamicSnippet}};
print("Deleted primary indices.");
}

// Now, delete replica indices
if (replicaIndices.isNotEmpty) {
List<MultipleBatchRequest> requests = replicaIndices.map((element) =>
MultipleBatchRequest(
action: Action.delete,
indexName: element.name
)
).toList();
await {{#dynamicSnippet}}deleteMultipleIndicesReplica{{/dynamicSnippet}};
print("Deleted replica indices.");
}
}

0 comments on commit e2d1c04

Please sign in to comment.