delete-by-query.asciidoc 18 KB

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