Deleting a field from a document in Elasticsearch

Exploring methods for deleting a field from a document in Elasticsearch.

In Elasticsearch, it is a common requirement to delete a field from a document. This can be useful when you want to remove unnecessary or outdated information from your index. In this article, we will discuss different methods to delete a field from a document in Elasticsearch, along with examples and step-by-step instructions.

Method 1: Using the Update API

The Update API allows you to update a document by providing a script that modifies the document’s source. You can use this API to delete a field from a document by setting the field to null. Here’s a step-by-step guide on how to do this:

1. Identify the index, document type (if using Elasticsearch 6.x or earlier), and document ID of the document you want to update.

2. Use the Update API with a script that sets the field to null, or even better, removes it from the source document. The following example demonstrates how to delete the “field_to_delete” field from a document with ID “1” in the “my_index” index:

POST /my_index/_update/1
{
  "script": "ctx._source.remove('field_to_delete')"
}

3. Execute the request. If successful, Elasticsearch will return a response indicating that the document has been updated.

Note: This method only removes the field from the specified document. The field will still exist in the mapping and other documents in the index.

Method 2: Reindexing with a Modified Source

If you want to delete a field from all documents in an index, you can use the Reindex API to create a new index with the modified source. Here’s how to do this:

1. Create a new index with the same settings and mappings as the original index. You can use the Get Index API to retrieve the settings and mappings of the original index.

2. Use the Reindex API to copy documents from the original index to the new index, while removing the field from the source. The following example demonstrates how to delete the “field_to_delete” field from all documents in the “my_index” index:

POST /_reindex
{
  "source": {
    "index": "my_index"
  },
  "dest": {
    "index": "new_index"
  },
  "script": {
    "source": "ctx._source.remove('field_to_delete')"
  }
}


3. Verify that the new index contains the correct documents with the field removed.

4. If everything looks good, you can delete the original index and, if necessary, add an alias to the new index having the name of the original index name.

Method 3: Updating the Mapping and Reindexing

If you want to delete a field from the mapping and all documents in an index, you can update the mapping and then reindex the documents. Here’s how to do this:

1. Create a new index with the same settings as the original index.

2. Retrieve the mappings of the original index using the Get Mapping API.

3. Modify the mappings by removing the field you want to delete.

4. Apply the modified mappings to the new index using the Put Mapping API.

5. Use the Reindex API to copy documents from the original index to the new index, as described in Method 2.

6. Verify that the new index contains the correct documents with the field removed and that the field is not present in the mapping.

7. If everything looks good, you can delete the original index and, if necessary, add an alias to the new index having the name of the original index name.

Conclusion

In this article, we discussed three methods to delete a field from a document in Elasticsearch: using the Update API, reindexing with a modified source, and updating the mapping and reindexing. Each method has its own use cases and trade-offs, so choose the one that best fits your requirements. Always remember to test your changes and verify the results before applying them to production environments.

Ready to try this out on your own? Start a free trial.

Want to get Elastic certified? Find out when the next Elasticsearch Engineer training is running!

Related content

Configuring recursive chunking for structured documents in Elasticsearch

November 11, 2025

Configuring recursive chunking for structured documents in Elasticsearch

Learn how to configure recursive chunking in Elasticsearch with chunk size, separator groups, and custom separator lists for optimal structured document indexing.

How to deploy Elasticsearch and Kibana on AWS EKS auto mode with ECK

November 10, 2025

How to deploy Elasticsearch and Kibana on AWS EKS auto mode with ECK

Learn how to deploy Elasticsearch and Kibana on Kubernetes using AWS EKS Auto Mode and ECK in this easy-to-follow guide.

Introducing Elasticsearch Query Rules UI in Kibana

Introducing Elasticsearch Query Rules UI in Kibana

Learn how to use the Elasticsearch Query Rules UI to add or exclude documents from search queries using customizable rulesets in Kibana, without affecting organic ranking.

How to install and configure Elasticsearch on AWS EC2

October 31, 2025

How to install and configure Elasticsearch on AWS EC2

Learn how to deploy Elasticsearch and Kibana on an Amazon EC2 instance in this step-by-step guide.

How to deploy Elasticsearch on an Azure Virtual Machine

October 29, 2025

How to deploy Elasticsearch on an Azure Virtual Machine

Learn how to deploy Elasticsearch on Azure VM with Kibana for full control over your Elasticsearch setup configuration.

Ready to build state of the art search experiences?

Sufficiently advanced search isn’t achieved with the efforts of one. Elasticsearch is powered by data scientists, ML ops, engineers, and many more who are just as passionate about search as your are. Let’s connect and work together to build the magical search experience that will get you the results you want.

Try it yourself