Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Deleting Documents in Elasticsearch

Introduction

In Elasticsearch, deleting documents is a common operation that you might need to perform for various reasons, such as data cleanup, data correction, or simply removing outdated information. This tutorial will guide you through the different methods available for deleting documents from an Elasticsearch index, step-by-step.

Prerequisites

Before we dive into deleting documents, ensure you have the following:

  • Elasticsearch installed and running
  • Basic understanding of Elasticsearch indexing
  • Access to an Elasticsearch index with documents

Deleting a Document by ID

You can delete a document from an index by specifying its ID. The DELETE request is used for this purpose.

Example

Assume you have an index named my_index and a document with ID 1. To delete this document, you can use the following command:

DELETE /my_index/_doc/1

The response should look something like this:

{ "_index": "my_index", "_type": "_doc", "_id": "1", "_version": 2, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 }

Deleting Documents by Query

Sometimes, you may need to delete multiple documents that match a specific query. This can be achieved using the _delete_by_query endpoint.

Example

To delete all documents from my_index where the field status is inactive, you can use the following command:

POST /my_index/_delete_by_query { "query": { "match": { "status": "inactive" } } }

The response will contain details about the deletion process:

{ "took": 123, "timed_out": false, "total": 10, "deleted": 10, "batches": 1, "version_conflicts": 0, "noops": 0, "retries": { "bulk": 0, "search": 0 }, "throttled_millis": 0, "requests_per_second": -1, "throttled_until_millis": 0, "failures": [] }

Deleting All Documents in an Index

If you want to remove all documents from an index, you can use the _delete_by_query endpoint with a match_all query.

Example

To delete all documents from my_index, use the following command:

POST /my_index/_delete_by_query { "query": { "match_all": {} } }

The response will look similar to the previous example, indicating the number of documents deleted.

Important Considerations

When deleting documents, keep the following points in mind:

  • Deleting documents by ID is more efficient than using _delete_by_query, especially for large datasets.
  • Using _delete_by_query can be resource-intensive and may impact the performance of your cluster.
  • Deleted documents are not immediately removed from disk; they are marked for deletion and will be removed during segment merging.

Conclusion

Deleting documents in Elasticsearch is a straightforward process, whether you're removing a single document by ID or multiple documents using a query. Understanding the methods and their implications can help you manage your Elasticsearch indices effectively.