Is there a way to delete documents based on _parent value

Hi All,

I have indexed parent and child documents , child document mapped to parent document based on _parent value.

How can i delete child documents which all have _parent value as 'XXX'?

Could you please share the query if any.

Thanks in advance.

you can use _delete_by_query:
POST index_name/child_type/_delete_by_query
{
 "query":
 {
  "match":
  {
   "_parent": "XXX"
  }
 }
}

Hi Vivek,

This will work fine incase of one child document. Let's say if i want to delete multiple child documents then what should be the query?

Am trying like,
{
"query" : {
"match" : {

                "_parent" : [1,2]
            }
        }

}

it's giving below error "{"error":{"root_cause":[{"type":"illegal_state_exception","reason":"Can't get text on a START_ARRAY at 1:30"}],"type":"illegal_state_exception","reason":"Can't get text on a START_ARRAY at 1:30"},"status":500}"

Just use terms instead of match:

POST index_name/child_type/_delete_by_query
{
 "query":
  {
    "terms": {
       "_parent": [1, 2]
     }
  }
}

Hi Vivek,

Above query not worked for me. I haven't get any error but response showing 0 records deleted.

I slightly changed above query(like below) and achieved my goal.

POST index_name/child_type/_delete_by_query
{
"query":
{
"terms": {
"_routing": [1, 2]
}
}
}

Thanks & Regards,
Pradeep Amara.

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