Reindex Drupal's Search API module indexes programmatically

I had recently found the need to manually reindex the Search API indexes programmatically after performing some other operation. The code snippet below will do just that for you :)

// Load Search API Index.
$search_api_index = search_api_index_load('SEARCH_API_INDEX_NAME');
// Clear the index.
$search_api_index->clear();
// Run!
search_api_index_items($search_api_index, -1);

Refer to Search API documentation here:
http://drupalcontrib.org/api/drupal/contributions!search_api!search_api....

By the way, this was tested working with Search API 7.x-1.6.

Hope this helps somebody out there!

4 comments for 'Reindex Drupal's Search API module indexes programmatically'

drunken monkey's picture

Just note that the third line,

<?php
$search_api_index
->reindex();
?>

is unnecessary, calling $search_api_index->clear() will already do the same thing.