You are here

public function Database::removeIndex in Search API 8

Removes an index from this server.

This might mean that the index has been deleted, or reassigned to a different server. If you need to distinguish between these cases, inspect $index->getServerId().

If the index wasn't added to the server previously, the method call should be ignored.

Implementations of this method should also check whether $index->isReadOnly() and don't delete any indexed data if it is.

Parameters

\Drupal\search_api\IndexInterface|string $index: Either an object representing the index to remove, or its ID (if the index was completely deleted).

Throws

\Drupal\search_api\SearchApiException Thrown if an error occurred while removing the index.

Overrides BackendPluginBase::removeIndex

File

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

Class

Database
Indexes and searches items using the database.

Namespace

Drupal\search_api_db\Plugin\search_api\backend

Code

public function removeIndex($index) {
  if (!is_object($index)) {

    // If the index got deleted, create a dummy to simplify the code. Since we
    // can't know, we assume the index was read-only, just to be on the safe
    // side.
    $index = Index::create([
      'id' => $index,
      'read_only' => TRUE,
    ]);
  }
  $db_info = $this
    ->getIndexDbInfo($index);
  try {
    if (!isset($db_info['field_tables']) && !isset($db_info['index_table'])) {
      return;
    }

    // Don't delete the index data of read-only indexes.
    if (!$index
      ->isReadOnly()) {
      foreach ($db_info['field_tables'] as $field) {
        if ($this->database
          ->schema()
          ->tableExists($field['table'])) {
          $this->database
            ->schema()
            ->dropTable($field['table']);
        }
      }
      if ($this->database
        ->schema()
        ->tableExists($db_info['index_table'])) {
        $this->database
          ->schema()
          ->dropTable($db_info['index_table']);
      }
    }
    $this
      ->getKeyValueStore()
      ->delete($index
      ->id());
  } catch (\Exception $e) {
    throw new SearchApiException($e
      ->getMessage(), $e
      ->getCode(), $e);
  }
}