You are here

public function Server::deleteAllItems in Search API 8

Deletes all items on this server, except those from read-only indexes.

Throws

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

Overrides ServerInterface::deleteAllItems

File

src/Entity/Server.php, line 437

Class

Server
Defines the search server configuration entity.

Namespace

Drupal\search_api\Entity

Code

public function deleteAllItems() {
  $failed = [];
  $properties['status'] = TRUE;
  $properties['read_only'] = FALSE;
  foreach ($this
    ->getIndexes($properties) as $index) {
    try {
      $this
        ->getBackend()
        ->deleteAllIndexItems($index);
      Cache::invalidateTags([
        'search_api_list:' . $index
          ->id(),
      ]);
    } catch (SearchApiException $e) {
      $args = [
        '%index' => $index
          ->label(),
      ];
      $this
        ->logException($e, '%type while deleting all items from index %index: @message in %function (line %line of %file).', $args);
      $failed[] = $index
        ->label();
    }
  }
  if (!empty($e)) {
    $server_name = $this
      ->label();
    $failed = implode(', ', $failed);
    throw new SearchApiException("Deleting all items from server '{$server_name}' failed for the following (write-enabled) indexes: {$failed}.", 0, $e);
  }
  $types = [
    'deleteItems',
    'deleteAllIndexItems',
  ];
  \Drupal::getContainer()
    ->get('search_api.server_task_manager')
    ->delete($this, NULL, $types);
}