Node data stays in index after node is deleted

Created on 26 April 2023, over 1 year ago
Updated 27 April 2023, over 1 year ago

Problem/Motivation

When an indexed node is deleted, it stays indexed.

Since the node is deleted it doesn't show up in results, but it pollutes the database.

Steps to reproduce

Index a node. Delete the node. Query the search_dataset table and see that the node is still indexed.

Proposed resolution

In #2925505: Re-indexing changed nodes is not triggered β†’ a number of hooks were added in order to mark updated nodes as needing to be re-indexed. We should add a hook_node_delete to clear deleted nodes from the index.

The Node class has this in its preDelete method:

// Ensure that all nodes deleted are removed from the search index.
    if (\Drupal::hasService('search.index')) {
      /** @var \Drupal\search\SearchIndexInterface $search_index */
      $search_index = \Drupal::service('search.index');
      foreach ($entities as $entity) {
        $search_index->clear('node_search', $entity->nid->value);
      }
    }

Comparing our update hook to that, our predelete would probably look something like this:

function search_exclude_node_delete(NodeInterface $node) {
  /** @var SearchPageRepository $repo */
  $repo = \Drupal::service('search.search_page_repository');
  /** @var SearchPageInterface $item */
  foreach ($repo->getIndexableSearchPages() as $item) {
    if ($item->getPlugin()->getPluginId() === 'search_exclude_node_search') {
      /** @var SearchExcludeNodeSearch $plugin */
      $plugin = $item->getPlugin();
      $plugin->searchIndex->clear('search_exclude_node_search', $node->id());
    }
  }
}

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024