Delete all documents from index/type without deleting type

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

I know one can delete all documents from a certain type via deleteByQuery.

Example:

curl -XDELETE 'http://localhost:9200/twitter/tweet/_query' -d '{     "query" : {         "term" : { "user" : "kimchy" }     } }'

But i have NO term and simply want to delete all documents from that type, no matter what term. What is best practice to achieve this? Empty term does not work.

Link to deleteByQuery

回答1:

I believe if you combine the delete by query with a match all it should do what you are looking for, something like this (using your example):

curl -XDELETE 'http://localhost:9200/twitter/tweet/_query' -d '{     "query" : {          "match_all" : {}     } }'

Or you could just delete the type:

curl -XDELETE http://localhost:9200/twitter/tweet


回答2:

The Delete-By-Query plugin has been removed in favor of a new Delete By Query API implementation in core. Read here

curl -XPOST 'localhost:9200/twitter/tweet/_delete_by_query?conflicts=proceed&pretty' -d' {     "query": {         "match_all": {}     } }'


回答3:

From ElasticSearch 5.x, delete_by_query API is there by default

POST: http://localhost:9200/index/type/_delete_by_query

{     "query": {          "match_all": {}     } }


回答4:

Torsten Engelbrecht's comment in John Petrones answer expanded:

curl -XDELETE 'http://localhost:9200/twitter/tweet/_query' -d    '{       "query":        {           "match_all": {}       }    }'

(I did not want to edit John's reply, since it got upvotes and is set as answer, and I might have introduced an error)



回答5:

Starting from Elasticsearch 2.x delete is not anymore allowed, since documents remain in the index causing index corruption.



回答6:

Note for ES2+

Starting with ES 1.5.3 the delete-by-query API is deprecated, and is completely removed since ES 2.0

Instead of the API, the Delete By Query is now a plugin.

In order to use the Delete By Query plugin you must install the plugin on all nodes of the cluster:

sudo bin/plugin install delete-by-query

All of the nodes must be restarted after the installation.


The usage of the plugin is the same as the old API. You don't need to change anything in your queries - this plugin will just make them work.


*For complete information regarding WHY the API was removed you can read more here.



回答7:

(Reputation not high enough to comment) The second part of John Petrone's answer works - no query needed. It will delete the type and all documents contained in that type, but that can just be re-created whenever you index a new document to that type.

Just to clarify: $ curl -XDELETE 'http://localhost:9200/twitter/tweet'

Note: this does delete the mapping! But as mentioned before, it can be easily re-mapped by creating a new document.



回答8:

You can delete documents from type with following query:

POST /index/type/_delete_by_query {     "query" : {          "match_all" : {}     } }

I tested this query in Kibana and Elastic 5.5.2



回答9:

In Kibana Console:

POST calls-xin-test-2/_delete_by_query {   "query": {      "match_all": {}   } }


回答10:

Just to add couple cents to this.

The "delete_by_query" mentioned at the top is still available as a plugin in elasticsearch 2.x.

Although in the latest upcoming version 5.x it will be replaced by "delete by query api"



回答11:

Elasticsearch 2.3 the option

    action.destructive_requires_name: true

in elasticsearch.yml do the trip

    curl -XDELETE http://localhost:9200/twitter/tweet


回答12:

1.) Using curl:- Delete a whole index:-

curl -XDELETE 'localhost:9200/indexName?pretty'
example) curl -XDELETE 'localhost:9200/mentorz?pretty'

for more details you can find here -https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html

2.) Using Delete by Query:- Delete a whole index:-

curl -XDELETE 'http://localhost:9200/mentorz/users/_query' -d
'{
"query":
{
"match_all": {}
}
}'

*Here mentorz-index name and users-type



回答13:

If you want to delete document according to a date. You can use kibana console (v.6.1.2)

POST index_name/_delete_by_query {       "query" : {               "range" : {                  "sendDate" : {                      "lte" : "2018-03-06"                               }                         }                   } }


回答14:

The above answers no longer work with ES 6.2.2 because of Strict Content-Type Checking for Elasticsearch REST Requests. The curl command which I ended up using is this:

curl -H'Content-Type: application/json' -XPOST 'localhost:9200/yourindex/_doc/_delete_by_query?conflicts=proceed' -d' { "query": { "match_all": {} }}'


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!