0
0
Elasticsearchquery~5 mins

Deleting documents in Elasticsearch

Choose your learning style9 modes available
Introduction

Deleting documents removes unwanted or outdated data from your search index. This keeps your data clean and relevant.

You want to remove a user profile that is no longer active.
You need to delete outdated product listings from your store.
You want to clear test data after running experiments.
You need to remove duplicate entries from your index.
Syntax
Elasticsearch
DELETE /index_name/_doc/document_id

This command deletes a single document by its ID from the specified index.

Make sure to replace index_name and document_id with your actual index and document ID.

Examples
Deletes the document with ID 123 from the 'products' index.
Elasticsearch
DELETE /products/_doc/123
Deletes all documents in 'products' index where the category is 'obsolete'.
Elasticsearch
POST /products/_delete_by_query
{
  "query": {
    "match": { "category": "obsolete" }
  }
}
Sample Program

This command deletes the document with ID 1 from the 'library' index.

Elasticsearch
DELETE /library/_doc/1
OutputSuccess
Important Notes

Deleting a document is permanent; it cannot be undone.

Use _delete_by_query to delete multiple documents matching a condition.

Check the response to confirm the document was deleted successfully.

Summary

Use the DELETE method with the document ID to remove a single document.

Use _delete_by_query to delete many documents based on a query.

Always verify the deletion response to ensure success.