Algolia does not 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 and prefix your test indices with, say, test_, you might find it useful to regularly purge all those indices.

Deleting one index

To delete a single index, you can make a direct deletion call to the specific index as follows:

1
$index->delete();

Deleting multiple indices

Deleting all indices

You can delete all your indices programmatically, by using one of our API clients and writing a script.

If you’re trying to delete replica indices, you must detach them from their primary index before deleting them.

The process is composed of four steps:

  1. List all your indices
  2. If replicas exist: Separate primary indices from replicas.
  3. Loop through the primary indices first and delete each.
  4. If replicas exist: Wait until all attached primaries are deleted so the replicas are no longer attached, then loop through them and delete each replica.
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);

Deleting a subset of indices

Deleting some indices is the same as deleting all (above), except you add a step to filter the full set of indices down to just 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, '_tmp') !== false) {
        array_push($ops, [
            'indexName' => $indexName,
            'action' => 'delete',
        ]);
    }
}

$res = $client->multipleBatch($ops);

Did you find this page helpful?