# Python - Delete all docs from a type

**URL:** <https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170>\
**Category:** Elasticsearch\
**Created:** [February 23, 2018, 5:46am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170 "2018-02-23T05:46:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaisharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaisharma/32/22833_2.png) [@jaisharma](https://discuss.elastic.co/u/jaisharma)\
**Post date:** [February 23, 2018, 5:46am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/1 "2018-02-23T05:46:14Z")

</div>

Elasticsearch version: 2.4.1

I'm using es python client and want to delete all documents matching a particular type. Currently i'm using helpers.scan to get all matching \_id followed by issuing a bulk delete request like this:

```
bulk_body = ['{"delete": {"_index": "talend.com", "_type": "2017-01-01", "_id": "1"}}',
'{"delete": {"_index": "talend.com", "_type": "2017-01-01", "_id": "2"}}',
'{"delete": {"_index": "talend.com", "_type": "2017-01-01", "_id": "3"}}']

es.bulk(bulk_body, request_timeout=50)

```

This doesn't look very efficient to me as the whole operation is done on client side which should be handled by es server.

On inspection i found out about [delete-by-query api](https://www.elastic.co/guide/en/elasticsearch/plugins/2.4/delete-by-query-usage.html) which is implemented using the [delete by query plugin](https://www.elastic.co/guide/en/elasticsearch/plugins/2.0/plugins-delete-by-query.html).

Now i'm unable to find a pythonic way to achieve the above functionality. This has already been answered [here](https://discuss.elastic.co/t/delete-all-entries-from-a-type/16642) but via a curl request.  
`

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 23, 2018, 6:36am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/2 "2018-02-23T06:36:39Z")

</div>

Is it a one time operation or something you are planning to do often ?

If the former, just run it in the Kibana dev console.  
If the later, please be aware that with 6.0, you can have only one type per index so removing a type is actually removing an index.

Finally you must know that it just removes the documents not the mapping and the way elasticsearch works it can be a costly operation. Often better to reindex without the type.

---

<div class="post-metadata">

**Author:** ![jaisharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaisharma/32/22833_2.png) [@jaisharma](https://discuss.elastic.co/u/jaisharma)\
**Post date:** [February 23, 2018, 6:52am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/3 "2018-02-23T06:52:20Z")

</div>

The system design in my case is such that each type is a date. Moving forward a new type is created each day and thus each document has its copy in each type.

Now the reason i want to delete docs from a type is **cleanup**. Basically in case of intra day updates, i'd like to drop the previous copy of all docs and recreate that type. I'm obviously not bothered about the mapping in this case and just want to cleanup the documents. This has to be done multiple times in a day resulting in deletion of around 1000 documents each time. Please note that i'm using **Elasticsearch==2.4.1**

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 23, 2018, 7:05am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/4 "2018-02-23T07:05:30Z")

</div>

That's a bad practice.  
Use time based indices.

---

<div class="post-metadata">

**Author:** ![jaisharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaisharma/32/22833_2.png) [@jaisharma](https://discuss.elastic.co/u/jaisharma)\
**Post date:** [February 23, 2018, 7:07am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/5 "2018-02-23T07:07:55Z")

</div>

We have index per customer and then time series data is split across types. Do you mean each index should be per customer per day? That'd lead to huge number of indices as we've data worth 3-4 years - approximately 50K indices !

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 23, 2018, 7:30am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/6 "2018-02-23T07:30:12Z")

</div>

Or put all your customers inside the same index, create filtered aliases per customer.

---

<div class="post-metadata">

**Author:** ![jaisharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaisharma/32/22833_2.png) [@jaisharma](https://discuss.elastic.co/u/jaisharma)\
**Post date:** [February 27, 2018, 6:05am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/7 "2018-02-27T06:05:49Z")

</div>

Filtered alias! i see. This might just work in our case. Does an alias function exactly the same as an index in terms of search or scroll? Can i just replace index functionality with alias and expect everything to fall in place?  
Also, how would filtered alias help me in cleanup i was talking about? When you delete an alias, the data is still retained in the base index. How do i get rid of it?

---

<div class="post-metadata">

**Author:** ![jaisharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaisharma/32/22833_2.png) [@jaisharma](https://discuss.elastic.co/u/jaisharma)\
**Post date:** [February 27, 2018, 6:10am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/8 "2018-02-27T06:10:17Z")

</div>

The way i'm issuing a bulk request to cleanup a particular type, is it considered a good practice? I saw some anomalies in production wrt this method as data was not getting deleted completely.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 27, 2018, 2:02pm UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/9 "2018-02-27T14:02:04Z")

</div>

If you have time based indices, cleaning old data by dropping the index is perfect.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 27, 2018, 2:02pm UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/10 "2018-02-27T14:02:17Z")

</div>

No. It's not recommended.

---

<div class="post-metadata">

**Author:** ![jaisharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaisharma/32/22833_2.png) [@jaisharma](https://discuss.elastic.co/u/jaisharma)\
**Post date:** [March 8, 2018, 9:20am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/11 "2018-03-08T09:20:29Z")

</div>

Any particular reason why its not recommended? Its not there in elasticsearch documentation.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 8, 2018, 11:00am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/12 "2018-03-08T11:00:13Z")

</div>

The way delete documents works will most likely increase first your data volume then eventually give you back your disk space after some IO intensive merge operation.

---

<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:** [April 5, 2018, 11:00am UTC](https://discuss.elastic.co/t/python-delete-all-docs-from-a-type/121170/13 "2018-04-05T11:00:52Z")

</div>

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