function search_index_clear

search_index_clear($type = NULL, $sid = NULL, $langcode = NULL)

Clears either a part of, or the entire search index.

This function is meant for use by search page plugins, or for building a user interface that lets users clear all or parts of the search index.

Parameters

string|null $type: (optional) The plugin ID or other machine-readable type for the items to remove from the search index. If omitted, $sid and $langcode are ignored and the entire search index is cleared.

string|null $sid: (optional) The ID of the items to remove from the search index. If omitted, all items matching $type are cleared, and $langcode is ignored.

string|null $langcode: (optional) Language code of the item to remove from the search index. If omitted, all items matching $sid and $type are cleared.

File

core/modules/search/search.module, line 134
Enables site-wide keyword searching.

Code

function search_index_clear($type = NULL, $sid = NULL, $langcode = NULL) {
  $query_index = db_delete('search_index');
  $query_dataset = db_delete('search_dataset');
  if ($type) {
    $query_index->condition('type', $type);
    $query_dataset->condition('type', $type);
    if ($sid) {
      $query_index->condition('sid', $sid);
      $query_dataset->condition('sid', $sid);
      if ($langcode) {
        $query_index->condition('langcode', $langcode);
        $query_dataset->condition('langcode', $langcode);
      }
    }
  }

  $query_index->execute();
  $query_dataset->execute();

  if ($type) {
    // Invalidate all render cache items that contain data from this index.
    Cache::invalidateTags(['search_index:' . $type]);
  }
  else {
    // Invalidate all render cache items that contain data from any index.
    Cache::invalidateTags(['search_index']);
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!search!search.module/function/search_index_clear/8.1.x