Guides / Sending and managing data / Manage your indices

Delete Multiple Indices

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
$index->delete();

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:

  1. List all your indices.
  2. Loop through all primary indices and delete them.
  3. 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);

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);

Did you find this page helpful?