Elastic Search

What are the Methods to Delete Documents From Elasticsearch

The document is a major part of Elasticsearch and is also referred to as a storage unit of data that preserves the data in JSON format. Like in typical relational and SQL databases, the document can be used as a row of a database that stores the data in different fields. Further, these documents are stored and managed by Elasticsearch indices/indexes.

In a typical database, rows and tables are managed through queries and users can easily perform CRUD operations on them. But in Elasticsearch, the data and documents are created, removed, and stored by Elasticsearch Rest APIs. Sometimes, users mistakenly enter the wrong data in the document and want to remove it from the index but do not know how to do that. Do not worry!

In this article, we will demonstrate the methods to remove or delete the document from the Elasticsearch index.

What are the Methods to Delete Documents From Elasticsearch?

In Elasticsearch, documents can be deleted by different approaches such as removing the document by id, or by using the delete query to delete the document. The other approach is to delete the whole index using DELETE API, but we do not recommend this method as it can delete all of the documents along with the index and users can lose important data.

The following are the methods to delete the document from the Elasticsearch index:

Method 1: Delete Document by Id in Elasticsearch

To delete the document by id, utilize the Elasticsearch “DELETE” API along with the index name and document id. For demonstration, go through the following instructions.

Step 1: Start Elasticsearch

Run Elasticsearch on the system. To do so, launch the Command Prompt terminal and open the Elasticsearch “bin” directory:

cd C:\Users\Dell\Documents\Elk stack\elasticsearch-8.9.0\bin

Then, run the batch file “elasticsearch.bat” to start the database:

elasticsearch.bat

When the cluster health changes from “RED” to “YELLOW”, it means Elasticsearch is started on the system:

Step 2: Start Kibana

When Elasticsearch is properly started, run the Kibana with the help of its batch file. To do so, first, utilize the “cd” command to launch Kibana “bin” directory:

cd C:\Users\Dell\Documents\Elk stack\kibana-8.9.0\bin

After that, run the batch file to start Kibana:

kibana.bat

Step 3: Sign In to Kibana

When Kibana is started, open the URL “localhost:5601”, add the login information for the Elasticsearch user, and hit the “Log in” button:

Step 4: Launch Kibana Console

In the next step, open the Kibana menu. Then, choose the “Dev Tools” option to launch Kibana’s console:

Step 5: Access all Documents of the Index

Upon doing so, the Kibana console will appear on the screen. Execute the “GET” API along with the index name and “_search” query to extract all the documents from the index:

GET /linuxhint/_search?pretty

Here, you can see we currently have two documents that have Ids “1” and “2” respectively. Let’s move forward to delete the document which has id “1”:

Step 6: Delete the Document by Id

To delete the document by id, utilize the “DELETE /<index-name>/_doc/<id>” API request as done below:

DELETE /linuxhint/_doc/1?pretty

In the above command, we are deleting a document having id “1” from the “linuxhint” index:

Step 7: Verification

For confirmation, again check out all the documents of Elasticsearch index using the below API request:

GET /linuxhint/_search?pretty

The below output shows that we have successfully deleted the document that has id “1”:

Method 2: Delete the Document by Delete Query in Elasticsearch

To delete the document based on a specific condition or using the “_delete_by_query” query, follow the given steps.

Step 1: Access All Documents of an Index

First, access all the documents from an index using the “GET” API request:

GET /linuxhint/_search?pretty

Step 2: Delete Documents by Query

In order to delete the data based on a specific condition or by “_delete_by_query” query, follow the below API request:

POST /linuxhint/_delete_by_query
{
"query": {
"match": {
"Designation": "Author"
}
}
}

In the above snippet, the “POST” API request is used to send or match the specific condition and “_delete_by_query” is used to delete the matched document. Here, we are deleting the document in which the “Designation” value is equal to “Author”:

Step 3: Verification

For verification, again list down all the documents using the below API request:

GET /linuxhint/_search?pretty

The output shows that we currently do not have any documents because both documents are deleted as they match the specified condition:

Bonus Tip: Delete Index From Elasticsearch

Deleting the index can delete the whole index along with all documents. We do not recommend deleting the index as users can lose important data. In order to delete the index, look at the provided steps.

Step 1: Delete Index

In order to delete the index completely in Elasticsearch, utilize “DELETE /<index-name>” API request:

DELETE /linuxhint

Step 2: Verification

For confirmation whether the index is deleted or not, utilize the “GET” API along with the index name:

GET /linuxhint

The output shows the “index not found” exception that indicates that we have deleted the “linuxhint” index effectively:

We have covered the methods to remove or delete the documents from Elasticsearch.

Conclusion

To delete the Documents, the user can either delete them by id or based on a specific condition by utilizing the “_delete_by_query” query. One other possible way is to delete the whole index in Elasticsearch which will delete all the documents along with an index. But we never recommend deleting the index completely as it can contain important documents. This post has demonstrated the techniques to remove the documents from 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.