Solr Delete documents functionality used in many situations like restructure solr schema, remove unwanted documents to reduce index size.In this article we will discuss various way of deleting documents from index.

Solr Delete by Id field

If want to delete documents by it’s id field.use below query


Solr Delete by other field

If want to delete documents that matches particular field,use that field in query.For example if we want to delete document which city is “Washington” then use below query.


Solr Delete by multiple fields

we can delete documents that matches multiple fields by specifying multiple fields.For example if want to delete document which city is “Washington” and “london” then use below query.

<delete><query>city:Washington AND city:london</query></delete>&commit=true

Solr Delete all documents

Match all docs query to delete all documents from solr index.


Solr Delete documents using Solrj client

We can also delete document using solrj client.Refer below code which demonstrate how to delete documents using solrj client.

import org.apache.Solr.client.Solrj.SolrClient; 
import org.apache.Solr.client.Solrj.SolrServerException; 
import org.apache.Solr.client.Solrj.impl.HttpSolrClient; 
import org.apache.Solr.common.SolrInputDocument;  
public class DeleteDocumentsExample { 
   public static void main(String args[]) throws SolrServerException, IOException {
      //Preparing the Solr client 
      String urlString = "http://localhost:8983/solr/DeleteDocumentExample"; 
      SolrClient solrClinent = new HttpSolrClient.Builder(urlString).build();   
      //Delete by id
      //Delete by other field
      //Deleting all documents
      //Saving the document 
      System.out.println("Documents deleted"); 

Refer solrj client , Reference guide for details.


Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *