# Triggering SOLR to Remove Records Already Deleted from the Database

Source: https://www.ironin.it/blog/triggering-solr-to-remove-records-already-deleted-from-the-database.html

Published 27 April 2018

[back-end development](https://www.ironin.it/blog/tags/back-end-development) [web](https://www.ironin.it/blog/tags/web) [database](https://www.ironin.it/blog/tags/database) [automation](https://www.ironin.it/blog/tags/automation)

![Triggering SOLR to Remove Records Already Deleted from the Database](https://www.ironin.it/api/media/file/ironin-seed-NWExNTMwYzAtYTFkMy00MzNkLWI3ZTMtYjdjMWI0YWUxM2Mz_triggering-solr-to-remove-records-already-deleted-from-db3.jpg)

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](https://lucene.apache.org/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:

```ruby
Model.search do
  # search criteria
end.results
```

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:

```ruby
results = Model.search do
  # search criteria
end.raw_results
```

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:

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

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:

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

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:

```ruby
def clean_up_model(model)
  results = model.search.raw_results
  results.each do |result|
    if model.find_by(id: result.primary_key).blank?
      Sunspot.remove_by_id(model, result.primary_key)
    end
  end
end
clean_up_model(Post)
clean_up_model(User)
```

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](https://www.ironin.it/contact-us.html). We love to help our clients find better, more efficient ways of running their systems!

Share:

Author: iRonin IT Team

Need software like this built?

[Software development →](https://www.ironin.it/ai-software-engineering)

Our code is merged into 10 open-source AI-agent projects, two of them with tens of thousands of GitHub stars.

[Talk to us →](https://www.ironin.it/contact-us.html?offer=ai-software-engineering)

## Read similar articles

- [Improve Performance and Save Money with These Software Development Tricks](https://www.ironin.it/blog/improve-performance-and-save-money-with-these-software-development-tricks.html)
- [GraphQL Part 4 - Build GraphQL API with Ruby on Rails](https://www.ironin.it/blog/graphql-part-4-api-with-ruby-on-rails.html)
- [How iRonin helps bring positive change by using cutting-edge fin-tech technologies](https://www.ironin.it/blog/how-ironin-helps-bring-positive-change-by-using-cutting-edge-fin-tech-technologies-go-elixir-react-native-sre.html)
- [Why Node.js is great choice for a SaaS project](https://www.ironin.it/blog/why-nodejs-is-great-choice-for-a-saas-project.html)

## Read next

- [Which AI model is best for your own task? Our own test and a public leaderboard disagreed](https://www.ironin.it/blog/own-ai-model-evals.html)
- [What uses up a Claude plan's usage limit? What each kind of token costs against it](https://www.ironin.it/blog/claude-usage-limit-token-costs.html)
- [Node.js - a disruptive technology that also helps to disrupt the finance, healthcare and travel industries](https://www.ironin.it/blog/nodejs-a-disruptive-technology-thats-also-helping-to-disrupt-the-finance-healthcare-and-travel-industries.html)
