# Is it possible to delete a document from a script?

**URL:** https://discuss.elastic.co/t/is-it-possible-to-delete-a-document-from-a-script/42343
**Category:** Elasticsearch
**Created:** [February 21, 2016, 7:13pm UTC](https://discuss.elastic.co/t/is-it-possible-to-delete-a-document-from-a-script/42343 "2016-02-21T19:13:50Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Attila\_Nagy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/attila_nagy/32/46906_2.png) [@Attila\_Nagy](https://discuss.elastic.co/u/Attila_Nagy)
#### Post date: [February 21, 2016, 7:13pm UTC](https://discuss.elastic.co/t/is-it-possible-to-delete-a-document-from-a-script/42343/1 "2016-02-21T19:13:50Z")

</div>

Hi,

I would like to delete the document which a script is called upon.  
The use case for this is I have a list of resources in the doc, which can grow or shrink, according to their availability.  
I want the doc to disappear when this list becomes empty, so when I call the script -let's name it removeResource-, it should simply delete the doc when this list becomes empty in the same transaction.  
Is this possible to do via scripting? How?

Are there any extended docs in scripting?

Thanks,

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [February 21, 2016, 8:08pm UTC](https://discuss.elastic.co/t/is-it-possible-to-delete-a-document-from-a-script/42343/2 "2016-02-21T20:08:58Z")

</div>

You can't do it in a single call. Since atomicity seems to be your main concern, you would have to do the following:

1. run a get operation on your document, this will in particular return a \_version
2. if the list of resources would be empty after the update then run a delete operation
3. otherwise run an index operation that removes a resource from the list

For steps 2 and 3, it is important to specify the version so that you can be sure to replace the exact same document that you got on step 1. And in case of a version conflict, you need to start again from step 1.

See [https://www.elastic.co/blog/elasticsearch-versioning-support](https://www.elastic.co/blog/elasticsearch-versioning-support) for more information about versioning.

---

<div class="post-metadata">

### Author: ![Attila\_Nagy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/attila_nagy/32/46906_2.png) [@Attila\_Nagy](https://discuss.elastic.co/u/Attila_Nagy)
#### Post date: [February 21, 2016, 9:03pm UTC](https://discuss.elastic.co/t/is-it-possible-to-delete-a-document-from-a-script/42343/3 "2016-02-21T21:03:00Z")

</div>

So there is no delete support in scripts. Are the any technical reasons for this, or just didn't yet happen? 🙂

I will do it this way, thank you very much for the quick response!

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [February 21, 2016, 9:56pm UTC](https://discuss.elastic.co/t/is-it-possible-to-delete-a-document-from-a-script/42343/4 "2016-02-21T21:56:39Z")

</div>

Indeed there is no way to have a script to run some kinds of "update-or-delete" operations. I don't think there is a particular reason to it, just that the current APIs cover most use-cases and that most other use-cases can be solved with several calls to the API.

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [February 28, 2016, 7:39pm UTC](https://discuss.elastic.co/t/is-it-possible-to-delete-a-document-from-a-script/42343/5 "2016-02-28T19:39:57Z")

</div>

Actually, it is both possible and documented:

```
PUT t/t/1
{}

POST t/t/1/_update
{
  "script": "ctx.op='delete'"
}

GET t/t/1
```

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [February 28, 2016, 7:54pm UTC](https://discuss.elastic.co/t/is-it-possible-to-delete-a-document-from-a-script/42343/6 "2016-02-28T19:54:11Z")

</div>

Woops my bad. Thanks Clinton for restoring the truth.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 11:13pm UTC](https://discuss.elastic.co/t/is-it-possible-to-delete-a-document-from-a-script/42343/7 "2017-07-05T23:13:03Z")

</div>


