

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.
Table of Contents
Solr Delete by Id field
If want to delete documents by it’s id field.use below query
http://localhost:8983/solr/update?stream.body= <delete><query>id:101</query></delete>&commit=true
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.
http://localhost:8983/solr/update?stream.body= <delete><query>city:Washington</query></delete>&commit=true
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.
http://localhost:8983/solr/update?stream.body= <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.
http://localhost:8983/solr/update?stream.body= <delete><query>*:*</query></delete>&commit=true
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 java.io.IOException; 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 //solrClinet.deleteByQuery("id:1"); //Delete by other field //solrClinet.deleteByQuery("city:Washington"); //Deleting all documents solrClinent.deleteByQuery("*:*"); //Saving the document solrClinent.commit(); System.out.println("Documents deleted"); } }
Refer solrj client , Reference guide for details.