Deletion will permanently remove an index and associated records from your application.
Deletion may not be your only option. If you want to reduce your usage, remove the records from an index (clear it) through either the dashboard’s Indices section or the API.
If you want to keep a backup of an index, export it. You can also save your index’s configuration data.
Deleting one index
To delete a single index, you can use the dashboard or API.
Delete an index using the dashboard
- Select an index.
- In the Browse tab, click the Manage index drop-down and select Delete.
- Confirm your request by typing DELETE into the pop-up box and then click Delete.
Delete an index using the API
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
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 using the API
You can’t delete multiple indices at once using the dashboard.
There isn’t a direct method to delete multiple indices, but you can write a script 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 purge them from your application from time to time.
Deleting all indices
- List all your indices.
- Loop through all primary indices and delete them.
- If you want to delete replica indices, you must detach them from their primary index. 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
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
| $indices = $client->listIndices();
// Delete primary indices only
$primaryIndices = array_filter($indices['items'], function ($index){
return !isset($index['primary']);
});
$ops = array_map(function ($index) {
return array(
'indexName' => $index['name'],
'action' => 'delete'
);
}, $primaryIndices);
$res = $client->multipleBatch($ops);
// Delete primary and replica indices. You need to delete the primary replicas first.
$primaryIndices = array_filter($indices['items'], function ($index){
return !isset($index['primary']);
});
$replicaIndices = array_diff($indices['items'], $primaryIndices);
$ops = array_map(function ($index) {
return array(
'indexName' => $index['name'],
'action' => 'delete'
);
}, $primaryIndices);
$res = $client->multipleBatch($ops);
$ops = array_map(function ($index) {
return array(
'indexName' => $index['name'],
'action' => 'delete'
);
}, $replicaIndices);
$res = $client->multipleBatch($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
| indices = client.list_indexes
# Delete primary indices only
primaries = indices['items'].select {|i| i['primary'].nil? }
ops = primaries.map do |index|
{ indexName: index['name'], action: 'delete' }
end
client.multiple_batch(ops)
# Delete primary and replica indices. You must delete primary indices first.
primaryIndices, replicaIndices = indices['items'].partition {|i| i.primary.nil? }
primaries = indices['items'].select {|i| i['primary'].nil? }
replicas = indices['items'] - primaries
ops = primaries.map do |index|
{ indexName: index['name'], action: 'delete' }
end
client.multiple_batch(ops)
ops = replicas.map do |index|
{ indexName: index['name'], action: 'delete' }
end
client.multiple_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',
};
});
return 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
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
| // Primary indices only
indices = client.list_indices()
primaries = []
for index in indices['items']:
if not 'primary' in index :
primaries.append({
'indexName': index['name'],
'action': 'delete',
})
res = client.multiple_batch(ops)
// Primary and replica indices
indices = client.list_indices()
primaries = []
replicas = []
for index in indices['items']:
if not 'primary' in index :
primaries.append({
'indexName': index['name'],
'action': 'delete',
})
else:
replicas.append({
'indexName': index['name'],
'action': 'delete',
})
// Delete primary and replica indices. You need to delete the primary indices first.
res = client.multiple_batch(primaries)
res = client.multiple_batch(replicas)
|
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
39
40
41
42
43
44
45
46
47
48
49
50
51
| // Primary indices only
client.listIndices { result in
guard case .success(let response) = result else {
return
}
let operations = response.items
.filter { $0.primary == nil }
.map(\.name)
.map { ($0, BatchOperation(action: .delete)) }
client.multipleBatchObjects(operations: operations) { result in
guard case .success(let response) = result else {
return
}
print(response.batchesResponse.objectIDs)
}
}
// Primary and replica indices
client.listIndices { result in
guard case .success(let response) = result else {
return
}
let primaryIndicesDeleteOperations = response.items
.filter { $0.primary == nil }
.map(\.name)
.map { ($0, BatchOperation(action: .delete)) }
let replicaIndicesDeleteOperations = response.items
.filter { $0.primary != nil }
.map(\.name)
.map { ($0, BatchOperation(action: .delete)) }
client.multipleBatchObjects(operations: primaryIndicesDeleteOperations) { result in
guard case .success(let response) = result else {
return
}
response.wait { result in
guard case .success = result else {
return
}
print("Done deleting primary indices")
client.multipleBatchObjects(operations: replicaIndicesDeleteOperations) { result in
guard case .success = result else {
return
}
print("Done deleting replica indices")
}
}
}
|
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
39
40
41
42
43
44
45
| // Primary indices only
var indices = client.ListIndices().Items;
var deletePrimaryOperations = new List<BatchOperation<string>>();
foreach (var index in indices)
{
if(index.primary !== null){
deletePrimaryOperations.Add(new BatchOperation<string>
{
IndexName = index.Name,
Action = BatchActionType.Delete
});
}
}
client.MultipleBatch(deletePrimaryOperations);
// Primary and replica indices
var indices = client.ListIndices().Items;
var deletePrimaryOperations = new List<BatchOperation<string>>();
var deleteReplicasOperations = new List<BatchOperation<string>>();
foreach (var index in indices)
{
if(index.primary !== null){
deletePrimaryOperations.Add(new BatchOperation<string>
{
IndexName = index.Name,
Action = BatchActionType.Delete
});
}else{
deleteReplicasOperations.Add(new BatchOperation<string>
{
IndexName = index.Name,
Action = BatchActionType.Delete
});
}
}
if(!IsEmpty(deletePrimaryOperations)) client.MultipleBatch(deletePrimaryOperations);
if(!IsEmpty(deleteReplicasOperations)) client.MultipleBatch(deleteReplicasOperations);
|
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
| // Primary indices only
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);
}
// Primary and replica indices
Map<Boolean, List<IndicesResponse>> indices =
client.listIndices().stream()
.collect(Collectors.partitioningBy(index -> index.getPrimary() == null));
List<IndicesResponse> primaryIndices = indices.get(true);
List<IndicesResponse> replicaIndices = indices.get(false);
if (!primaryIndices.isEmpty()) {
client
.multipleBatch(
primaryIndices.stream()
.map(index -> BatchOperation.createDelete(index.getName()))
.collect(Collectors.toList()))
.waitTask();
if (!replicaIndices.isEmpty()) {
client
.multipleBatch(
replicaIndices.stream()
.map(index -> BatchOperation.createDelete(index.getName()))
.collect(Collectors.toList()))
.waitTask();
}
}
|
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
39
40
41
42
43
44
45
| // Primary indices only
listIndicesRes, err := client.ListIndices()
var ops []search.BatchOperationIndexed
for _, index := range listIndicesRes.Items {
if index.Primary != "" {
ops = append(ops, search.BatchOperationIndexed{
IndexName: index.Name,
BatchOperation: search.BatchOperation{Action: search.Delete},
})
}
}
res, err := client.MultipleBatch(ops)
// Primary and replica indices
listIndicesRes, err := client.ListIndices()
var deletePrimaryOps []search.BatchOperationIndexed
var deleteReplicasOps []search.BatchOperationIndexed
for _, index := range listIndicesRes.Items {
if index.Primary == "" {
deleteReplicasOps = append(deleteReplicasOps, search.BatchOperationIndexed{
IndexName: index.Name,
BatchOperation: search.BatchOperation{Action: search.Delete},
})
} else {
deletePrimaryOps = append(deletePrimaryOps, search.BatchOperationIndexed{
IndexName: index.Name,
BatchOperation: search.BatchOperation{Action: search.Delete},
})
}
}
if len(deletePrimaryOps) > 0 {
res, err := client.MultipleBatch(deletePrimaryOps)
}
if len(deleteReplicasOps) > 0 {
res, err := client.MultipleBatch(deleteReplicasOps)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| // Primary indices only
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)
// Primary and replica indices
val indices: Indices = Await.result(client.execute(list indices), Duration.Inf)
val (primaryIndices, replicaIndices) = indices.items.partition(_.primary == null)
if (primaryIndices.nonEmpty) {
val operations: Seq[DeleteIndexDefinition] = primaryIndices.map(i => delete index i.name)
val task: AlgoliaTask = Await.result(client.execute(batch(operations)), Duration.Inf)
if (replicaIndices.nonEmpty) {
val operations: Seq[DeleteIndexDefinition] = replicaIndices.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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| // Primary indices only
val indices = client.listIndices().items.filter { it.primaryOrNull == null }
if (indices.isNotEmpty()) {
val operations = indices.map {
BatchOperationIndex(it.indexName, BatchOperation.DeleteIndex)
}
client.multipleBatchObjects(operations)
}
// Primary and replica indices
val (primaryIndices, replicaIndices) = indices.partition { it.primaryOrNull == null }
if (primaryIndices.isNotEmpty()) {
val operations = primaryIndices.map {
BatchOperationIndex(it.indexName, BatchOperation.DeleteIndex)
}
client.multipleBatchObjects(operations).wait()
print("Done deleting primary indices")
if (replicaIndices.isNotEmpty()) {
val deleteOperations = replicaIndices.map {
BatchOperationIndex(it.indexName, BatchOperation.DeleteIndex)
}
client.multipleBatchObjects(deleteOperations).wait()
print("Done deleting replica indices")
}
}
|
Deleting some indices
Deleting some indices is like deleting them all, except you must add a step to select 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
12
13
14
15
16
| client.listIndices { result in
guard case .success(let response) = result else {
return
}
let deleteOperations: [(IndexName, BatchOperation)] = response.items
.map(\.name)
.filter { $0.rawValue.contains("test_") }
.map { ($0, .delete) }
client.multipleBatchObjects(operations: deleteOperations) { result in
guard case .success = result else {
return
}
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)
}
|