Elastic Search

How to Delete Index API Within Elasticsearch

Elasticsearch is a NoSQL and more complex tool than other databases. It provides us simple to use API support to store, manage, retrieve, and delete data. It is frequently used to store and manage unstructured and semi-structured data and store data in the form of indexes. Sometimes, the Elastic user wants to delete an index that the user may not need any longer, maybe redundant by creating an index with the same name but minor modifications, or may want to delete the index and its associated data. In such a scenario, Elasticsearch provides us with a DELETE index API to delete the whole index.

This blog will demonstrate how to delete Index API within Elasticsearch.

How to Delete Index API Within Elasticsearch?

Deleting index or indices means the removal of the index, shard, and index metadata. But to delete the index, you are required to roll over the Elasticsearch data stream to create a new index. To delete the index from Elasticsearch using the “DELETE” index API, go through the following instructions.

Step 1: Start Elasticsearch

First, launch the Command Prompt on your system. Then, navigate to the “bin” directory of Elasticsearch setup through the “cd” command:

cd C:\Users\Dell\Documents\Elasticsearch\elasticsearch-8.7.0\bin

After that, execute the “elasticsearch.bat” command to start the Elasticsearch engine on your localhost:

elasticsearch.bat

Step 2: Navigate Elasticsearch Default Port

Next, check if Elasticsearch is running on the default localhost port or not. To do so, utilize the “curl” command along with “localhost:9200”. The “-u” is utilized to provide the Elasticsearch user’s credentials:

curl localhost:9200 -u elastic:<user-password>

Step 3: List All Indexes

Next, list down all indexes of Elasticsearch by utilizing the methods command:

curl http://localhost:9200/_aliases?pretty=true -u elastic:<user-password>

From the output, you can see that we have currently four indexes, “test”, “employee”, “user”, and “my-index-000001”:

Step 4: Check Index Data

Access the data of the index that you want to delete to check if any important data exists in that index. For doing so, utilize the “GET” API request along with “localhost:9200/index-name/data-type?pretty”:

curl -X GET "localhost:9200/employee/_doc/Rafia?pretty" -u elastic:<user-password>

Step 5: Delete Index

Next, to delete the index, simply use the “DELETE” index API along with the “localhost:9200/index-name?pretty” command as shown below:

curl -X DELETE "localhost:9200/employee?pretty" -u elastic:<user-password>

In the output, the “acknowledged” value “true” means we have successfully deleted the Elasticsearch index:

For the verification, again list down all Elasticsearch indexes and check whether the chosen index is removed from the list or not:

curl http://localhost:9200/_aliases?pretty=true -u elastic:<user-password>

The below output indicates that we have successfully removed the “employee” index from Elasticsearch:

That’s all about deleting the Elasticsearch index using the DELETE index API.

Conclusion

To delete the Index by using DELETE index API, first, start Elasticsearch. Then, list down the index and check if the index exists that you want to delete. After that, delete the whole index using the “curl -X DELETE “localhost:9200/employee?pretty”” command. This blog has illustrated the technique to delete the Index API within Elasticsearch.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.