Published on 2012-04-26 by John Collins. Please follow me on Twitter for more:
When developing a search project using Apache Solr, you often need to clear out the search index and start again. A typical example of this is when you make a schema change. Luckily, there is an easy way to do this via the Solr REST API.
According to the official Solr FAQ, you can clear out a Solr index by issuing the following query:
To run this query, you can issue it via the REST API using curl:
curl http://localhost:8080/solr/update -H "Content-type: text/xml" \ --data-binary '<delete><query>*:*</query></delete>'
Note that you will also need to tell Solr to commit this change:
curl http://localhost:8080/solr/update -H "Content-type: text/xml" \ --data-binary '<commit />'
...and it is also a good idea to tell Solr to optimize it's index at this point:
curl http://localhost:8080/solr/update -H "Content-type: text/xml" \ --data-binary '<optimize />'
To confirm that your index is now empty, load up the Solr admin web interface in your browser, and click on the statistics link. You should now see that the number of documents listed in your index is now zero.
Note that the final optimize command is passed to Solr to tell it to physically remove the deleted documents, which will not only free up disc space, but will also increase search performance. Following the above procedure of delete/commit/optimize will ensure that you start off with a clean slate each time.