Algolia doesn’t provide a direct method to delete multiple indices, only one at a time. However, you can write your own script using our API clients to loop through and delete the indices you want.
For example, if you’re running tests with indices that you generated with the prefix test_
, you might want to regularly purge them from your application.
Deleting one index
To delete a single index, you can use the deleteIndex
method on the index you want to delete.
1
2
3
| index.delete().then(() => {
// done
});
|
1
2
3
4
5
| index.delete { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
|
1
| client.deleteIndexAsync("myIndex", null);
|
1
2
3
4
| index.Delete();
// Asynchronous
await index.DeleteAsync();
|
1
2
3
4
5
| //Sync version
index.delete();
//Async version
index.deleteAsync();
|
1
| res, err := index.Delete()
|
1
| client.execute { delete index "index" }
|
Deleting multiple indices
Deleting all indices
While we don’t provide a built-in method to delete all indices in an application, you can write your own script to do it programmatically. If you want to delete replica indices as well, you first need to detach them from their primary index.
The process is as follows:
- List all your indices.
- Loop through all primary indices and delete them.
- If there are replicas, wait for the deletion of their primary indices for them to automatically detach, then loop through and delete the replicas.
1
2
3
4
5
6
7
8
9
10
11
| $indices = $client->listIndices();
$ops = array();
foreach ($indices['items'] as $index) {
array_push($ops, [
'indexName' => $index['name'],
'action' => 'delete',
]);
}
$res = $client->multipleBatch($ops);
|
1
2
3
4
5
6
7
8
9
10
11
| indices = client.list_indexes
ops = []
indices['items'].each do |index|
ops.push({
indexName: index['name'],
action: 'delete'
})
end
res = client.batch(ops)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| // Primary indices only
client.listIndices().then(({ items }) => {
const ops = items
.filter(({ name, primary }) => !primary)
.map(({ name }) => {
return {
indexName: name,
action: 'delete',
};
});
client.multipleBatch(ops).then(({ objectIDs }) => {
console.log(objectIDs);
});
});
// Primary and replica indices
client.listIndices().then(({ items }) => {
const { primaryOps, replicaOps } = items.reduce(
(memo, { name, primary }) => {
memo[primary ? 'primaryOps' : 'replicaOps'].push({
indexName: name,
action: 'delete',
});
return memo;
},
{ primaryOps: [], replicaOps: [] }
);
return client
.multipleBatch(primaryOps)
.wait()
.then(() => {
console.log('Done deleting primary indices');
return client.multipleBatch(replicaOps).then(() => {
console.log('Done deleting replica indices');
});
});
});
|
1
2
3
4
5
6
7
8
9
10
| indices = client.list_indices()
ops = []
for index in indices['items']:
ops.append({
'indexName': index['name'],
'action': 'delete',
})
res = client.multiple_batch(ops)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| var indices = client.ListIndices().Items;
var operations = new List<BatchOperation<string>>();
foreach (var index in indices)
{
operations.Add(new BatchOperation<string>
{
IndexName = index.Name,
Action = BatchActionType.Delete
});
}
client.MultipleBatch(operations);
|
1
2
3
4
5
6
7
8
9
10
11
| List<IndicesResponse> indices = client.listIndices();
if (!indices.isEmpty()) {
List<BatchOperation<Object>> operations =
indices.stream()
.map(i -> new BatchOperation<>(i.getName(), ActionEnum.DELETE))
.collect(Collectors.toList());
client.multipleBatch(operations);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
| listIndicesRes, err := client.ListIndices()
var ops []search.BatchOperationIndexed
for _, index := range listIndicesRes.Items {
ops = append(ops, search.BatchOperationIndexed{
IndexName: index.Name,
BatchOperation: search.BatchOperation{Action: search.Delete},
})
}
res, err := client.MultipleBatch(ops)
|
1
2
3
| val indices: Indices = Await.result(client.execute(list indices), Duration.Inf)
val operations: Seq[DeleteIndexDefinition] = indices.items.map(i => delete index i.name)
val task: AlgoliaTask = Await.result(client.execute(batch(operations)), Duration.Inf)
|
1
2
3
4
5
6
7
8
9
| val indices = client.listIndices().items
if (indices.isNotEmpty()) {
val operations = indices.map {
BatchOperationIndex(it.indexName, BatchOperation.DeleteIndex)
}
client.multipleBatchObjects(operations)
}
|
Deleting a subset of indices
Deleting some indices is similar to deleting them all, except you need to add a step to filter the full set down to only the indices you want.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| $indices = $client->listIndices();
$ops = array();
foreach ($indices['items'] as $index) {
$indexName = $index['name'];
if (strpos($indexName, 'test_') !== false) {
array_push($ops, [
'indexName' => $indexName,
'action' => 'delete',
]);
}
}
$res = $client->multipleBatch($ops);
|
1
2
3
4
5
6
7
8
9
10
11
| indices = client.list_indexes
ops = []
indices['items'].each do |index|
index_name = index['name']
next unless index_name.include? 'test_'
ops.push({ indexName: index_name, action: 'delete' })
end
res = client.batch(ops)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| client.listIndices().then(({ items }) => {
const ops = items
.filter(({ name }) => name.includes('test_'))
.map(({ name }) => {
return {
indexName: name,
action: 'delete',
};
});
client.multipleBatch(ops).then(({ objectIDs }) => {
console.log(objectIDs);
});
});
|
1
2
3
4
5
6
7
8
9
10
11
12
| indices = client.list_indices()
ops = []
for index in indices['items']:
index_name = index['name']
if 'test_' in index_name:
ops.append({
'indexName': index_name,
'action': 'delete',
})
res = client.multiple_batch(ops)
|
1
2
3
4
5
6
7
8
9
10
11
| let indices = client.listIndices { result in
if case .success(let response) = result {
let indices = response.items
let indexOperations: [(IndexName, BatchOperation)] = indices.map { ($0.name, .delete) }
client.multipleBatchObjects(operations: indexOperations) { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| var indices = client.ListIndices().Items.Where(x =>
x.Name.Contains($"tmp_")).ToList();
var operations = new List<BatchOperation<string>>();
foreach (var index in indices)
{
operations.Add(new BatchOperation<string>
{
IndexName = index.Name,
Action = BatchActionType.Delete
});
}
client.MultipleBatch(operations);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| List<IndicesResponse> indices =
client.listIndices().stream()
.filter(
i -> i.getName().contains("test_"))
.collect(Collectors.toList());
if (!indices.isEmpty()) {
List<BatchOperation<Object>> operations =
indices.stream()
.map(i -> new BatchOperation<>(i.getName(), ActionEnum.DELETE))
.collect(Collectors.toList());
client.multipleBatch(operations);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| listIndicesRes, err := client.ListIndices()
var ops []search.BatchOperationIndexed
for _, index := range listIndicesRes.Items {
indexName := index.Name
if strings.Contains(indexName, "test_") {
ops = append(ops, search.BatchOperationIndexed{
IndexName: indexName,
BatchOperation: search.BatchOperation{Action: search.Delete},
})
}
}
res, err := client.MultipleBatch(ops)
|
1
2
3
| val indices: Indices = Await.result(client.execute(list indices), Duration.Inf)
val operations: Seq[DeleteIndexDefinition] = indices.items.filter(_.name.endsWith("test_")).map(i => delete index i.name)
val task: AlgoliaTask = Await.result(client.execute(batch(operations)), Duration.Inf)
|
1
2
3
4
5
6
7
8
9
10
11
| val indices = client.listIndices().items.filter {
it.indexName.raw.contains("test_")
}
if (indices.isNotEmpty()) {
val operations = indices.map {
BatchOperationIndex(it.indexName, BatchOperation.DeleteIndex)
}
client.multipleBatchObjects(operations)
}
|