Triggering SOLR to Remove Records Already Deleted from the Database

iRonin IT Team - Experts in software development

When you’re using Solr, sometimes the search can index records that have already been removed from your database - causing some frustration. Here’s how to remove these hanging records from Solr so you have a clean search again.

Solr is an open source enterprise search platform. However, like any piece of software, there can be certain conditions or situations where it may not perform as you wish or expect.

This article looks at one of these cases: Sometimes your Solr search engine contains indexed records that are no longer actually present in the database. It’s very hard to detect these kind of records during a regular search, because they don’t have any actual representation in the database.

In most cases you would run a search similar to this: do
  # search criteria

Even if there is a record matching your search criteria, it would not be returned - because we receive a collection of model instances as the results.

To bypass this, we have to ask Solr about raw records instead:

results = do
  # search criteria

This time, we receive an array of Sunspot::Search::Hit objects from which we can easily get each record’s primary key, even if the record is no longer present in the database:

ids = []
results.each do |result|
  ids << result.primary_key

Because we now have an array of primary keys, we can easily clear our storage of records that are no longer present in our database, by checking for blank primary keys and then removing any that exist:

ids.each do |id|
  unless Model.exists?(id)
    Sunspot.remove_by_id(Model, id)

It is good idea to create a rake task and run it systematically to remove these hanging records. Below you can find the function that you can use for clearing records from any of your models:

def clean_up_model(model)
  results =
  results.each do |result|
    if model.find_by(id: result.primary_key).blank?
      Sunspot.remove_by_id(model, result.primary_key)


We like to share these types of fixes so that others can get the solution quickly and easily if they find themselves in the same situation. If you have some strange behaviors in your enterprise software or infrastructure and would like an expert eye over the issue, then please get in touch. We love to help our clients find better, more efficient ways of running their systems!

Author's Bio
iRonin IT Team

Experts in software development

We are a 100% remote team of software development experts, providing web & mobile application development and DevOps services for international clients.

Similar articles

Bulletproof your development with remote team augmentation

Read how
This page is best viewed in portrait mode
Our websites and web services use cookies. We use cookies and collected data to enhance your experience, provide additional communication channels, improve marketing materials and enhance our offer. IRONIN SP. Z O.O. SP. K. is committed to protecting all the data that we collect or process in any way, especially data of personal nature. By accepting these terms you agree to our usage of cookies and processing your data, according to our Privacy Policy, and you declare that your browser settings reflect your preferences. Read more You have the right to revoke this agreement at any time, based on the terms of our Privacy Policy. You can change cookies settings in your browser. If you do not agree with us using cookies and processing your data, please change your cookies settings in your web browser and reject these terms. You can find more information about cookies, your data privacy This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies. data processing, and your rights in our Privacy Policy.