Delete documents from ElasticSearch index in python

3.3k Views Asked by At

Using elasticsearch-py, I would like to remove all documents from a specific index, without removing the index. Given that delete_by_query was moved to a separate plugin, I want to know what is the best way to go about this?

2

There are 2 best solutions below

2
On BEST ANSWER

Use a Scroll/Scan API call to gather all Document IDs and then call batch delete on those IDs. This is the recommended replacement for the Delete By Query API based on the official documentation.

EDIT: Requested information for using this specifically in elasticsearch-py. Here is the documentation for the helpers. Use the Scan helper to scan throgh all documents. Use the Bulk helper with the delete action to delete all the ids.

1
On

It is highly inefficient to delete all the docs by delete by query. More direct and correct action is:

  • Getting the current mappings (Assuming you are not using index templates)
  • Dropping the index by DELETE /indexname
  • Creating the new index and the mappings.

This will take a second, former will take much, much more time and unnecessary disk I/O