delete-by-query.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. [[docs-delete-by-query]]
  2. == Delete By Query API
  3. experimental[The delete-by-query API is new and should still be considered experimental. The API may change in ways that are not backwards compatible]
  4. The simplest usage of `_delete_by_query` just performs a deletion on every
  5. document that match a query. Here is the API:
  6. [source,js]
  7. --------------------------------------------------
  8. POST twitter/_delete_by_query
  9. {
  10. "query": { <1>
  11. "match": {
  12. "message": "some message"
  13. }
  14. }
  15. }
  16. --------------------------------------------------
  17. // CONSOLE
  18. // TEST[setup:big_twitter]
  19. <1> The query must be passed as a value to the `query` key, in the same
  20. way as the <<search-search,Search API>>. You can also use the `q`
  21. parameter in the same way as the search api.
  22. That will return something like this:
  23. [source,js]
  24. --------------------------------------------------
  25. {
  26. "took" : 147,
  27. "timed_out": false,
  28. "deleted": 119,
  29. "batches": 1,
  30. "version_conflicts": 0,
  31. "noops": 0,
  32. "retries": {
  33. "bulk": 0,
  34. "search": 0
  35. },
  36. "throttled_millis": 0,
  37. "requests_per_second": -1.0,
  38. "throttled_until_millis": 0,
  39. "total": 119,
  40. "failures" : [ ]
  41. }
  42. --------------------------------------------------
  43. // TESTRESPONSE[s/"took" : 147/"took" : "$body.took"/]
  44. `_delete_by_query` gets a snapshot of the index when it starts and deletes what
  45. it finds using `internal` versioning. That means that you'll get a version
  46. conflict if the document changes between the time when the snapshot was taken
  47. and when the delete request is processed. When the versions match the document
  48. is deleted.
  49. NOTE: Since `internal` versioning does not support the value 0 as a valid
  50. version number, documents with version equal to zero cannot be deleted using
  51. `_delete_by_query` and will fail the request.
  52. During the `_delete_by_query` execution, multiple search requests are sequentially
  53. executed in order to find all the matching documents to delete. Every time a batch
  54. of documents is found, a corresponding bulk request is executed to delete all
  55. these documents. In case a search or bulk request got rejected, `_delete_by_query`
  56. relies on a default policy to retry rejected requests (up to 10 times, with
  57. exponential back off). Reaching the maximum retries limit causes the `_delete_by_query`
  58. to abort and all failures are returned in the `failures` of the response.
  59. The deletions that have been performed still stick. In other words, the process
  60. is not rolled back, only aborted. While the first failure causes the abort, all
  61. failures that are returned by the failing bulk request are returned in the `failures`
  62. element; therefore it's possible for there to be quite a few failed entities.
  63. If you'd like to count version conflicts rather than cause them to abort then
  64. set `conflicts=proceed` on the url or `"conflicts": "proceed"` in the request body.
  65. Back to the API format, you can limit `_delete_by_query` to a single type. This
  66. will only delete `tweet` documents from the `twitter` index:
  67. [source,js]
  68. --------------------------------------------------
  69. POST twitter/tweet/_delete_by_query?conflicts=proceed
  70. {
  71. "query": {
  72. "match_all": {}
  73. }
  74. }
  75. --------------------------------------------------
  76. // CONSOLE
  77. // TEST[setup:twitter]
  78. It's also possible to delete documents of multiple indexes and multiple
  79. types at once, just like the search API:
  80. [source,js]
  81. --------------------------------------------------
  82. POST twitter,blog/tweet,post/_delete_by_query
  83. {
  84. "query": {
  85. "match_all": {}
  86. }
  87. }
  88. --------------------------------------------------
  89. // CONSOLE
  90. // TEST[s/^/PUT twitter\nPUT blog\n/]
  91. If you provide `routing` then the routing is copied to the scroll query,
  92. limiting the process to the shards that match that routing value:
  93. [source,js]
  94. --------------------------------------------------
  95. POST twitter/_delete_by_query?routing=1
  96. {
  97. "query": {
  98. "range" : {
  99. "age" : {
  100. "gte" : 10
  101. }
  102. }
  103. }
  104. }
  105. --------------------------------------------------
  106. // CONSOLE
  107. // TEST[setup:twitter]
  108. By default `_delete_by_query` uses scroll batches of 1000. You can change the
  109. batch size with the `scroll_size` URL parameter:
  110. [source,js]
  111. --------------------------------------------------
  112. POST twitter/_delete_by_query?scroll_size=5000
  113. {
  114. "query": {
  115. "term": {
  116. "user": "kimchy"
  117. }
  118. }
  119. }
  120. --------------------------------------------------
  121. // CONSOLE
  122. // TEST[setup:twitter]
  123. [float]
  124. === URL Parameters
  125. In addition to the standard parameters like `pretty`, the Delete By Query API
  126. also supports `refresh`, `wait_for_completion`, `wait_for_active_shards`, and `timeout`.
  127. Sending the `refresh` will refresh all shards involved in the delete by query
  128. once the request completes. This is different than the Delete API's `refresh`
  129. parameter which causes just the shard that received the delete request
  130. to be refreshed.
  131. If the request contains `wait_for_completion=false` then Elasticsearch will
  132. perform some preflight checks, launch the request, and then return a `task`
  133. which can be used with <<docs-delete-by-query-task-api,Tasks APIs>>
  134. to cancel or get the status of the task. Elasticsearch will also create a
  135. record of this task as a document at `.tasks/task/${taskId}`. This is yours
  136. to keep or remove as you see fit. When you are done with it, delete it so
  137. Elasticsearch can reclaim the space it uses.
  138. `wait_for_active_shards` controls how many copies of a shard must be active
  139. before proceeding with the request. See <<index-wait-for-active-shards,here>>
  140. for details. `timeout` controls how long each write request waits for unavailable
  141. shards to become available. Both work exactly how they work in the
  142. <<docs-bulk,Bulk API>>.
  143. `requests_per_second` can be set to any positive decimal number (`1.4`, `6`,
  144. `1000`, etc) and throttles the number of requests per second that the delete-by-query
  145. issues or it can be set to `-1` to disabled throttling. The throttling is done
  146. waiting between bulk batches so that it can manipulate the scroll timeout. The
  147. wait time is the difference between the time it took the batch to complete and
  148. the time `requests_per_second * requests_in_the_batch`. Since the batch isn't
  149. broken into multiple bulk requests large batch sizes will cause Elasticsearch
  150. to create many requests and then wait for a while before starting the next set.
  151. This is "bursty" instead of "smooth". The default is `-1`.
  152. [float]
  153. === Response body
  154. The JSON response looks like this:
  155. [source,js]
  156. --------------------------------------------------
  157. {
  158. "took" : 639,
  159. "deleted": 0,
  160. "batches": 1,
  161. "version_conflicts": 2,
  162. "retries": 0,
  163. "throttled_millis": 0,
  164. "failures" : [ ]
  165. }
  166. --------------------------------------------------
  167. `took`::
  168. The number of milliseconds from start to end of the whole operation.
  169. `deleted`::
  170. The number of documents that were successfully deleted.
  171. `batches`::
  172. The number of scroll responses pulled back by the the delete by query.
  173. `version_conflicts`::
  174. The number of version conflicts that the delete by query hit.
  175. `retries`::
  176. The number of retries that the delete by query did in response to a full queue.
  177. `throttled_millis`::
  178. Number of milliseconds the request slept to conform to `requests_per_second`.
  179. `failures`::
  180. Array of all indexing failures. If this is non-empty then the request aborted
  181. because of those failures. See `conflicts` for how to prevent version conflicts
  182. from aborting the operation.
  183. [float]
  184. [[docs-delete-by-query-task-api]]
  185. === Works with the Task API
  186. You can fetch the status of any running delete-by-query requests with the
  187. <<tasks,Task API>>:
  188. [source,js]
  189. --------------------------------------------------
  190. GET _tasks?detailed=true&actions=*/delete/byquery
  191. --------------------------------------------------
  192. // CONSOLE
  193. The responses looks like:
  194. [source,js]
  195. --------------------------------------------------
  196. {
  197. "nodes" : {
  198. "r1A2WoRbTwKZ516z6NEs5A" : {
  199. "name" : "r1A2WoR",
  200. "transport_address" : "127.0.0.1:9300",
  201. "host" : "127.0.0.1",
  202. "ip" : "127.0.0.1:9300",
  203. "attributes" : {
  204. "testattr" : "test",
  205. "portsfile" : "true"
  206. },
  207. "tasks" : {
  208. "r1A2WoRbTwKZ516z6NEs5A:36619" : {
  209. "node" : "r1A2WoRbTwKZ516z6NEs5A",
  210. "id" : 36619,
  211. "type" : "transport",
  212. "action" : "indices:data/write/delete/byquery",
  213. "status" : { <1>
  214. "total" : 6154,
  215. "updated" : 0,
  216. "created" : 0,
  217. "deleted" : 3500,
  218. "batches" : 36,
  219. "version_conflicts" : 0,
  220. "noops" : 0,
  221. "retries": 0,
  222. "throttled_millis": 0
  223. },
  224. "description" : ""
  225. }
  226. }
  227. }
  228. }
  229. }
  230. --------------------------------------------------
  231. <1> this object contains the actual status. It is just like the response json
  232. with the important addition of the `total` field. `total` is the total number
  233. of operations that the reindex expects to perform. You can estimate the
  234. progress by adding the `updated`, `created`, and `deleted` fields. The request
  235. will finish when their sum is equal to the `total` field.
  236. With the task id you can look up the task directly:
  237. [source,js]
  238. --------------------------------------------------
  239. GET /_tasks/taskId:1
  240. --------------------------------------------------
  241. // CONSOLE
  242. // TEST[catch:missing]
  243. The advantage of this API is that it integrates with `wait_for_completion=false`
  244. to transparently return the status of completed tasks. If the task is completed
  245. and `wait_for_completion=false` was set on it them it'll come back with a
  246. `results` or an `error` field. The cost of this feature is the document that
  247. `wait_for_completion=false` creates at `.tasks/task/${taskId}`. It is up to
  248. you to delete that document.
  249. [float]
  250. [[docs-delete-by-query-cancel-task-api]]
  251. === Works with the Cancel Task API
  252. Any Delete By Query can be canceled using the <<tasks,Task Cancel API>>:
  253. [source,js]
  254. --------------------------------------------------
  255. POST _tasks/task_id:1/_cancel
  256. --------------------------------------------------
  257. // CONSOLE
  258. The `task_id` can be found using the tasks API above.
  259. Cancellation should happen quickly but might take a few seconds. The task status
  260. API above will continue to list the task until it is wakes to cancel itself.
  261. [float]
  262. [[docs-delete-by-query-rethrottle]]
  263. === Rethrottling
  264. The value of `requests_per_second` can be changed on a running delete by query
  265. using the `_rethrottle` API:
  266. [source,js]
  267. --------------------------------------------------
  268. POST _delete_by_query/task_id:1/_rethrottle?requests_per_second=-1
  269. --------------------------------------------------
  270. // CONSOLE
  271. The `task_id` can be found using the tasks API above.
  272. Just like when setting it on the `_delete_by_query` API `requests_per_second`
  273. can be either `-1` to disable throttling or any decimal number
  274. like `1.7` or `12` to throttle to that level. Rethrottling that speeds up the
  275. query takes effect immediately but rethrotting that slows down the query will
  276. take effect on after completing the current batch. This prevents scroll
  277. timeouts.
  278. [float]
  279. [[docs-delete-by-query-manual-slice]]
  280. === Manually slicing
  281. Delete-by-query supports <<sliced-scroll>> allowing you to manually parallelize
  282. the process relatively easily:
  283. [source,js]
  284. ----------------------------------------------------------------
  285. POST twitter/_delete_by_query
  286. {
  287. "slice": {
  288. "id": 0,
  289. "max": 2
  290. },
  291. "query": {
  292. "range": {
  293. "likes": {
  294. "lt": 10
  295. }
  296. }
  297. }
  298. }
  299. POST twitter/_delete_by_query
  300. {
  301. "slice": {
  302. "id": 1,
  303. "max": 2
  304. },
  305. "query": {
  306. "range": {
  307. "likes": {
  308. "lt": 10
  309. }
  310. }
  311. }
  312. }
  313. ----------------------------------------------------------------
  314. // CONSOLE
  315. // TEST[setup:big_twitter]
  316. Which you can verify works with:
  317. [source,js]
  318. ----------------------------------------------------------------
  319. GET _refresh
  320. POST twitter/_search?size=0&filter_path=hits.total
  321. {
  322. "query": {
  323. "range": {
  324. "likes": {
  325. "lt": 10
  326. }
  327. }
  328. }
  329. }
  330. ----------------------------------------------------------------
  331. // CONSOLE
  332. // TEST[continued]
  333. Which results in a sensible `total` like this one:
  334. [source,js]
  335. ----------------------------------------------------------------
  336. {
  337. "hits": {
  338. "total": 0
  339. }
  340. }
  341. ----------------------------------------------------------------
  342. // TESTRESPONSE
  343. [float]
  344. [[docs-delete-by-query-automatic-slice]]
  345. === Automatic slicing
  346. You can also let delete-by-query automatically parallelize using
  347. <<sliced-scroll>> to slice on `_uid`:
  348. [source,js]
  349. ----------------------------------------------------------------
  350. POST twitter/_delete_by_query?refresh&slices=5
  351. {
  352. "query": {
  353. "range": {
  354. "likes": {
  355. "lt": 10
  356. }
  357. }
  358. }
  359. }
  360. ----------------------------------------------------------------
  361. // CONSOLE
  362. // TEST[setup:big_twitter]
  363. Which you also can verify works with:
  364. [source,js]
  365. ----------------------------------------------------------------
  366. POST twitter/_search?size=0&filter_path=hits.total
  367. {
  368. "query": {
  369. "range": {
  370. "likes": {
  371. "lt": 10
  372. }
  373. }
  374. }
  375. }
  376. ----------------------------------------------------------------
  377. // CONSOLE
  378. // TEST[continued]
  379. Which results in a sensible `total` like this one:
  380. [source,js]
  381. ----------------------------------------------------------------
  382. {
  383. "hits": {
  384. "total": 0
  385. }
  386. }
  387. ----------------------------------------------------------------
  388. // TESTRESPONSE
  389. Adding `slices` to `_delete_by_query` just automates the manual process used in
  390. the section above, creating sub-requests which means it has some quirks:
  391. * You can see these requests in the
  392. <<docs-delete-by-query-task-api,Tasks APIs>>. These sub-requests are "child"
  393. tasks of the task for the request with `slices`.
  394. * Fetching the status of the task for the request with `slices` only contains
  395. the status of completed slices.
  396. * These sub-requests are individually addressable for things like cancellation
  397. and rethrottling.
  398. * Rethrottling the request with `slices` will rethrottle the unfinished
  399. sub-request proportionally.
  400. * Canceling the request with `slices` will cancel each sub-request.
  401. * Due to the nature of `slices` each sub-request won't get a perfectly even
  402. portion of the documents. All documents will be addressed, but some slices may
  403. be larger than others. Expect larger slices to have a more even distribution.
  404. * Parameters like `requests_per_second` and `size` on a request with `slices`
  405. are distributed proportionally to each sub-request. Combine that with the point
  406. above about distribution being uneven and you should conclude that the using
  407. `size` with `slices` might not result in exactly `size` documents being
  408. `_delete_by_query`ed.
  409. * Each sub-requests gets a slightly different snapshot of the source index
  410. though these are all taken at approximately the same time.
  411. [float]
  412. [[docs-delete-by-query-picking-slices]]
  413. === Picking the number of slices
  414. At this point we have a few recommendations around the number of `slices` to
  415. use (the `max` parameter in the slice API if manually parallelizing):
  416. * Don't use large numbers. `500` creates fairly massive CPU thrash.
  417. * It is more efficient from a query performance standpoint to use some multiple
  418. of the number of shards in the source index.
  419. * Using exactly as many shards as are in the source index is the most efficient
  420. from a query performance standpoint.
  421. * Indexing performance should scale linearly across available resources with
  422. the number of `slices`.
  423. * Whether indexing or query performance dominates that process depends on lots
  424. of factors like the documents being reindexed and the cluster doing the
  425. reindexing.