You are here

public function Server::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

File

src/Entity/Server.php, line 392

Class

Server
Defines the search server configuration entity.

Namespace

Drupal\search_api\Entity

Code

public function deleteAllIndexItems(IndexInterface $index, $datasource_id = NULL) {
  if ($index
    ->isReadOnly()) {
    $vars = [
      '%index' => $index
        ->label(),
    ];
    $this
      ->getLogger()
      ->warning('Trying to delete items from index %index which is marked as read-only.', $vars);
    return;
  }
  $server_task_manager = \Drupal::getContainer()
    ->get('search_api.server_task_manager');
  if (!$datasource_id) {

    // If we're deleting all items of the index, there's no point in keeping
    // any other "delete items" tasks.
    $types = [
      'deleteItems',
      'deleteAllIndexItems',
    ];
    $server_task_manager
      ->delete($this, $index, $types);
  }
  try {
    if ($server_task_manager
      ->execute($this)) {
      $this
        ->getBackend()
        ->deleteAllIndexItems($index, $datasource_id);

      // Clear search api list caches.
      Cache::invalidateTags([
        'search_api_list:' . $index
          ->id(),
      ]);
      return;
    }
  } catch (SearchApiException $e) {
    $vars = [
      '%server' => $this
        ->label(),
      '%index' => $index
        ->label(),
    ];
    $this
      ->logException($e, '%type while deleting items of index %index from server %server: @message in %function (line %line of %file).', $vars);
  }
  $task_manager = \Drupal::getContainer()
    ->get('search_api.task_manager');
  $task_manager
    ->addTask(__FUNCTION__, $this, $index, $datasource_id);
}