Last Updated: February 25, 2016
·
3.501K
· robertomiranda

How to clear an elasticsearch index in Ruby

The only thing that you need is run the following script in your ruby console

require "json"
keys = JSON.parse(`curl http://localhost:9200/_mapping`).keys
keys.each {|key| `curl -XDELETE http://localhost:9200/#{key}`}

Let's see in details how the ruby script above works, Basically the script take advantage of the elasticsearch.

This line fetch all the "mapping" from elasticsearch using the enpoint "/_mapping" and saved them on mapping.json file

curl http://localhost:9200/_mapping

Then the script parse the results fetched and saved into keys varaible and we iterate over the parent index names and start to call the endpoint for delete them

keys.each {|key| `curl -XDELETE http://localhost:9200/#{key}`}