1
0

delete.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. [[docs-delete]]
  2. === Delete API
  3. ++++
  4. <titleabbrev>Delete</titleabbrev>
  5. ++++
  6. Removes a JSON document from the specified index.
  7. [[docs-delete-api-request]]
  8. ==== {api-request-title}
  9. `DELETE /<index>/_doc/<_id>`
  10. [[docs-delete-api-desc]]
  11. ==== {api-description-title}
  12. You use DELETE to remove a document from an index. You must specify the
  13. index name and document ID.
  14. NOTE: You cannot send deletion requests directly to a data stream. To delete a
  15. document in a data stream, you must target the backing index containing the
  16. document. See <<update-delete-docs-in-a-backing-index>>.
  17. [discrete]
  18. [[optimistic-concurrency-control-delete]]
  19. ===== Optimistic concurrency control
  20. Delete operations can be made conditional and only be performed if the last
  21. modification to the document was assigned the sequence number and primary
  22. term specified by the `if_seq_no` and `if_primary_term` parameters. If a
  23. mismatch is detected, the operation will result in a `VersionConflictException`
  24. and a status code of 409. See <<optimistic-concurrency-control>> for more details.
  25. [discrete]
  26. [[delete-versioning]]
  27. ===== Versioning
  28. Each document indexed is versioned. When deleting a document, the `version` can
  29. be specified to make sure the relevant document we are trying to delete is
  30. actually being deleted and it has not changed in the meantime. Every write
  31. operation executed on a document, deletes included, causes its version to be
  32. incremented. The version number of a deleted document remains available for a
  33. short time after deletion to allow for control of concurrent operations. The
  34. length of time for which a deleted document's version remains available is
  35. determined by the `index.gc_deletes` index setting and defaults to 60 seconds.
  36. [discrete]
  37. [[delete-routing]]
  38. ===== Routing
  39. If routing is used during indexing, the routing value also needs to be
  40. specified to delete a document.
  41. If the `_routing` mapping is set to `required` and no routing value is
  42. specified, the delete API throws a `RoutingMissingException` and rejects
  43. the request.
  44. For example:
  45. ////
  46. Example to delete with routing
  47. [source,console]
  48. --------------------------------------------------
  49. PUT /my-index-000001/_doc/1?routing=shard-1
  50. {
  51. "test": "test"
  52. }
  53. --------------------------------------------------
  54. ////
  55. [source,console]
  56. --------------------------------------------------
  57. DELETE /my-index-000001/_doc/1?routing=shard-1
  58. --------------------------------------------------
  59. // TEST[continued]
  60. This request deletes the document with id `1`, but it is routed based on the
  61. user. The document is not deleted if the correct routing is not specified.
  62. [discrete]
  63. [[delete-index-creation]]
  64. ===== Automatic index creation
  65. If an <<docs-index_,external versioning variant>> is used,
  66. the delete operation automatically creates the specified index if it does not
  67. exist. For information about manually creating indices, see
  68. <<indices-create-index,create index API>>.
  69. [discrete]
  70. [[delete-distributed]]
  71. ===== Distributed
  72. The delete operation gets hashed into a specific shard id. It then gets
  73. redirected into the primary shard within that id group, and replicated
  74. (if needed) to shard replicas within that id group.
  75. [discrete]
  76. [[delete-wait-for-active-shards]]
  77. ===== Wait for active shards
  78. When making delete requests, you can set the `wait_for_active_shards`
  79. parameter to require a minimum number of shard copies to be active
  80. before starting to process the delete request. See
  81. <<index-wait-for-active-shards,here>> for further details and a usage
  82. example.
  83. [discrete]
  84. [[delete-refresh]]
  85. ===== Refresh
  86. Control when the changes made by this request are visible to search. See
  87. <<docs-refresh>>.
  88. [discrete]
  89. [[delete-timeout]]
  90. ===== Timeout
  91. The primary shard assigned to perform the delete operation might not be
  92. available when the delete operation is executed. Some reasons for this
  93. might be that the primary shard is currently recovering from a store
  94. or undergoing relocation. By default, the delete operation will wait on
  95. the primary shard to become available for up to 1 minute before failing
  96. and responding with an error. The `timeout` parameter can be used to
  97. explicitly specify how long it waits. Here is an example of setting it
  98. to 5 minutes:
  99. [source,console]
  100. --------------------------------------------------
  101. DELETE /my-index-000001/_doc/1?timeout=5m
  102. --------------------------------------------------
  103. // TEST[setup:my_index]
  104. [[docs-delete-api-path-params]]
  105. ==== {api-path-parms-title}
  106. `<index>`::
  107. (Required, string) Name of the target index.
  108. `<_id>`::
  109. (Required, string) Unique identifier for the document.
  110. [[docs-delete-api-query-params]]
  111. ==== {api-query-parms-title}
  112. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=if_seq_no]
  113. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=if_primary_term]
  114. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=pipeline]
  115. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=refresh]
  116. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  117. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeout]
  118. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=doc-version]
  119. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=version_type]
  120. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  121. [[docs-delete-api-example]]
  122. ==== {api-examples-title}
  123. Delete the JSON document `1` from the `my-index-000001` index:
  124. [source,console]
  125. --------------------------------------------------
  126. DELETE /my-index-000001/_doc/1
  127. --------------------------------------------------
  128. // TEST[setup:my_index]
  129. The API returns the following result:
  130. [source,console-result]
  131. --------------------------------------------------
  132. {
  133. "_shards": {
  134. "total": 2,
  135. "failed": 0,
  136. "successful": 2
  137. },
  138. "_index": "my-index-000001",
  139. "_id": "1",
  140. "_version": 2,
  141. "_primary_term": 1,
  142. "_seq_no": 5,
  143. "result": "deleted"
  144. }
  145. --------------------------------------------------
  146. // TESTRESPONSE[s/"successful": 2/"successful": 1/]
  147. // TESTRESPONSE[s/"_primary_term": 1/"_primary_term": $body._primary_term/]
  148. // TESTRESPONSE[s/"_seq_no": 5/"_seq_no": $body._seq_no/]