# Delete documents by query with values from other index

**URL:** https://discuss.elastic.co/t/delete-documents-by-query-with-values-from-other-index/214400
**Category:** Elasticsearch
**Created:** [January 9, 2020, 9:54am UTC](https://discuss.elastic.co/t/delete-documents-by-query-with-values-from-other-index/214400 "2020-01-09T09:54:25Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![johanwallenborg](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@johanwallenborg](https://discuss.elastic.co/u/johanwallenborg)
#### Post date: [January 9, 2020, 9:54am UTC](https://discuss.elastic.co/t/delete-documents-by-query-with-values-from-other-index/214400/1 "2020-01-09T09:54:26Z")

</div>

Hello,

I've created a similar question before which didn't get any answers so I create a new topic but with more information / code that maybe helps to understand what i'm after.

I've made an example of what I want to achive. I want to delete documents from dummyindex-stuff-1 if there is a document in dummyindex-cars-1 with that value in the Name-property. Does this require that I use .NET or Python API?

```
PUT dummyindex-stuff-1/_bulk?refresh
{"index":{"_id":1}}
{"objectName":"Banana","weight":1}
{"index":{"_id":2}}
{"objectName":"Volvo","weight":1000}
{"index":{"_id":3}}
{"objectName":"Tesla","weight":1000}
{"index":{"_id":4}}
{"objectName":"Ford","weight":1000}
{"index":{"_id":5}}
{"objectName":"Lemon","weight":1}
{"index":{"_id":6}}
{"objectName":"Orange","weight":1}

PUT dummyindex-cars-1/_bulk?refresh
{"index":{"_id":1}}
{"Name":"Banana"}
{"index":{"_id":2}}
{"Name":"Volvo"}
{"index":{"_id":3}}
{"Name":"Tesla"}

```

// Johan

---

<div class="post-metadata">

### Author: ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)
#### Post date: [January 9, 2020, 10:05am UTC](https://discuss.elastic.co/t/delete-documents-by-query-with-values-from-other-index/214400/2 "2020-01-09T10:05:36Z")

</div>

> [@johanwallenborg](#):
>
> Does this require that I use .NET or Python API

From what I understand in your ask, this doesn't require using a particular API like .NET or Python, but I think it cannot be done without using some sort of client logic. Assuming dummyindex-cars-1 is more or less static, you would iterate over all `Name` values (or if there is only one value per doc iterate over all docs) and delete the corresponding documents from the second index. In practise this would possible use some variation of the [scroll API](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-request-body.html#request-body-search-scroll) to get at the set of `Name` values, and then maybe batch some of those to perform the deletion e.g. with [delete-by-query](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html).

---

<div class="post-metadata">

### Author: ![johanwallenborg](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@johanwallenborg](https://discuss.elastic.co/u/johanwallenborg)
#### Post date: [January 9, 2020, 1:43pm UTC](https://discuss.elastic.co/t/delete-documents-by-query-with-values-from-other-index/214400/3 "2020-01-09T13:43:43Z")

</div>

> [@cbuescher](#):
>
> From what I understand in your ask, this doesn't require using a particular API like .NET or Python, but I think it cannot be done without using some sort of client logic. Assuming dummyindex-cars-1 is more or less static, you would iterate over all `Name` values (or if there is only one value per doc iterate over all docs) and delete the corresponding documents from the second index. In practise this would possible use some variation of the [scroll API](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-request-body.html#request-body-search-scroll) to get at the set of `Name` values, and then maybe batch some of those to perform the deletion e.g. with [delete-by-query](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html).

Thanks for answer 🙂

I tried this and it seem to work except that don't find any info on if it's a possible to fetch an array of Doc Id's

```
POST dummyindex-stuff-1/_delete_by_query?scroll=1m
{
  "query": {
    "terms": {
      "objectName.keyword": {
        "index" : "dummyindex-cars-1",
        "id": "3",
        "path": "Name.keyword"
      }
    }
  }
}

```

---

<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: [February 6, 2020, 1:43pm UTC](https://discuss.elastic.co/t/delete-documents-by-query-with-values-from-other-index/214400/4 "2020-02-06T13:43:50Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
