You are here

public function SearchApiSolrDevelCommands::deleteAll in Search API Solr 8.3

Same name and namespace in other branches
  1. 4.x modules/search_api_solr_devel/src/Commands/SearchApiSolrDevelCommands.php \Drupal\search_api_solr_devel\Commands\SearchApiSolrDevelCommands::deleteAll()

Deletes *all* documents on a Solr search server (including all indexes).

@command search-api-solr:devel-delete-all

@usage search-api-solr-devel:delete-all server_id Deletes *all* documents on server_id.

Parameters

string $server_id: The ID of the server.

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\search_api_solr\SearchApiSolrException

\Drupal\search_api\SearchApiException

File

modules/search_api_solr_devel/src/Commands/SearchApiSolrDevelCommands.php, line 66

Class

SearchApiSolrDevelCommands
Defines Drush commands for the Search API Solr Devel.

Namespace

Drupal\search_api_solr_devel\Commands

Code

public function deleteAll($server_id) {
  $servers = $this->commandHelper
    ->loadServers([
    $server_id,
  ]);
  if ($server = reset($servers)) {
    $backend = $server
      ->getBackend();
    if ($backend instanceof SolrBackendInterface) {
      $connector = $backend
        ->getSolrConnector();
      $update_query = $connector
        ->getUpdateQuery();
      $update_query
        ->addDeleteQuery('*:*');
      $connector
        ->update($update_query);
      foreach ($server
        ->getIndexes() as $index) {
        if ($index
          ->status() && !$index
          ->isReadOnly()) {
          if ($connector
            ->isCloud()) {
            $connector
              ->update($update_query, $backend
              ->getCollectionEndpoint($index));
          }
          $index
            ->reindex();
        }
      }
    }
    else {
      throw new SearchApiSolrException("The given server ID doesn't use the Solr backend.");
    }
  }
  else {
    throw new SearchApiException("The given server ID doesn't exist.");
  }
}