Solr XML Document Delete

We can use the delete command to delete the documents which are stored in Apache Solr index. We can delete the documents based on the document ID by specifying them in the tag or by specifying the field name. We can also delete the complete document using the *:* in between the query tag *:* .

We have already added the moneydata.xml XML document in Apache Solr index in the Document Addition section which has the following records.

<add> <doc> <field name="id">USD</field> <field name="name">One Dollar</field> <field name="manu">Bank of America</field> <field name="manu_id_s">boa</field> <field name="cat">currency</field> <field name="features">Coins and notes</field> <field name="price_c">1,USD</field> <field name="inStock">true</field> </doc> <doc> <field name="id">EUR</field> <field name="name">One Euro</field> <field name="manu">European Union</field> <field name="manu_id_s">eu</field> <field name="cat">currency</field> <field name="features">Coins and notes</field> <field name="price_c">1,EUR</field> <field name="inStock">true</field> </doc> <doc> <field name="id">GBP</field> <field name="name">One British Pound</field> <field name="manu">U.K.</field> <field name="manu_id_s">uk</field> <field name="cat">currency</field> <field name="features">Coins and notes</field> <field name="price_c">1,GBP</field> <field name="inStock">true</field> </doc> <doc> <field name="id">NOK</field> <field name="name">One Krone</field> <field name="manu">Bank of Norway</field> <field name="manu_id_s">nor</field> <field name="cat">currency</field> <field name="features">Coins and notes</field> <field name="price_c">1,NOK</field> <field name="inStock">true</field> </doc> </add>

Now we will see how to delete indexed records based on the ID or field name of the document in the following section.

Deleting Documents from Solr Index based on Document ID

We can delete the documents from Apache Solr Index using the ID of the document.

In the below example we will delete the Solr index document for those records which are having ID=USD. For that, we will create an XML file with the name deleteID_moneydata.xml under the Solr Home /bin directory and put the following records in that file.

<delete> <id>USD</id> </delete>

Now we will use the deleteID_moneydata.xml XML file to delete the records that have the ID=USD. Please note that we will use the Solr_sample_core core to perform this operation. The Solr_sample_core was created during the Core creation & deletion section of the tutorial.


cloudduggu@ubuntu:~/hadoop/solr-8.8.1/bin$ ./post -c Solr_sample_core deleteID_moneydata.xml


Once the command is executed successfully, we will see the below output.

solr id deletion cloudduggu

We can verify the deletion of the document from the Apache Solr Web interface, we can click on execute query button and see that the document with ID=USD has been deleted from the Solr index.

solr delete query output cloudduggu

Deleting Documents from Solr Index based on a Field

We can delete a document from the Solr index based on a field value. For example, if we want to delete a document that has a file name like Emp_Work_Location: Deoria.

The field name should be specified in the tag as mentioned below. We have created an XML file named deletefield_moneydata.xml and put the below statements in that.

<delete> <query>Emp_Work_Location:Deoria</query> </delete>

Now, let us run the Post command to delete the document with matching field names from the Solr index.


cloudduggu@ubuntu:~/hadoop/solr-8.8.1/bin$ ./post -c Solr_sample_core deletefield_moneydata.xml


The below output is generated once the command is completed successfully.


Deleting All Documents from Solr Index

We can delete all documents from the Apache Solr Index bypassing the *:* in the delete tag. To perform this operation we have created an XML file name deleteall.xml and we will pass it to the Post command as mentioned below.


cloudduggu@ubuntu:~/hadoop/solr-8.8.1/bin$ ./post -c Solr_sample_core deleteall.xml


The following message we can see after the command execution.

solr delete all command cloudduggu

We can verify it from the Apache Solr Web interface by Querying. Click on the Query option and leave everything default after that click on the Execute Query button. We will see that all documents which were stored under the Solr Core Solr_sample_core are deleted now.

solr all document delete cloudduggu