You are here

public function Database::deleteAllIndexItems in Search API 8

Deletes all the items from the index.

Parameters

\Drupal\search_api\IndexInterface $index: The index for which items should be deleted.

string|null $datasource_id: (optional) If given, only delete items from the datasource with the given ID.

Throws

\Drupal\search_api\SearchApiException Thrown if an error occurred while trying to delete indexed items.

Overrides BackendSpecificInterface::deleteAllIndexItems

1 call to Database::deleteAllIndexItems()
Database::fieldsUpdated in modules/search_api_db/src/Plugin/search_api/backend/Database.php
Updates the storage tables when the field configuration changes.

File

modules/search_api_db/src/Plugin/search_api/backend/Database.php, line 1585

Class

Database
Indexes and searches items using the database.

Namespace

Drupal\search_api_db\Plugin\search_api\backend

Code

public function deleteAllIndexItems(IndexInterface $index, $datasource_id = NULL) {
  try {
    $db_info = $this
      ->getIndexDbInfo($index);
    $datasource_field = $db_info['field_tables']['search_api_datasource']['column'];
    foreach ($db_info['field_tables'] as $field_id => $field) {
      if (!$datasource_id) {
        $this->database
          ->truncate($field['table'])
          ->execute();
        unset($db_info['field_tables'][$field_id]['multi-valued']);
      }
      else {
        if (!isset($query)) {
          $query = $this->database
            ->select($db_info['index_table'], 't')
            ->fields('t', [
            'item_id',
          ])
            ->condition($datasource_field, $datasource_id);
        }
        $this->database
          ->delete($field['table'])
          ->condition('item_id', clone $query, 'IN')
          ->execute();
      }
    }
    if (!$datasource_id) {
      $this
        ->getKeyValueStore()
        ->set($index
        ->id(), $db_info);
      $this->database
        ->truncate($db_info['index_table'])
        ->execute();
    }
    else {
      $this->database
        ->delete($db_info['index_table'])
        ->condition($datasource_field, $datasource_id)
        ->execute();
    }
  } catch (\Exception $e) {

    // The database operations might throw PDO or other exceptions, so we
    // catch them all and re-wrap them appropriately.
    throw new SearchApiException($e
      ->getMessage(), $e
      ->getCode(), $e);
  }
}