# Want to delete documents from elasticsearch which are deleted from database

**URL:** <https://discuss.elastic.co/t/want-to-delete-documents-from-elasticsearch-which-are-deleted-from-database/213334>\
**Category:** Elasticsearch\
**Created:** [December 30, 2019, 11:28am UTC](https://discuss.elastic.co/t/want-to-delete-documents-from-elasticsearch-which-are-deleted-from-database/213334 "2019-12-30T11:28:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![milankamboya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/milankamboya/32/47041_2.png) [@milankamboya](https://discuss.elastic.co/u/milankamboya)\
**Post date:** [December 30, 2019, 11:28am UTC](https://discuss.elastic.co/t/want-to-delete-documents-from-elasticsearch-which-are-deleted-from-database/213334/1 "2019-12-30T11:28:58Z")

</div>

Hi All,

I want to delete records from Elasticsearch for below use-case. What are the possible resolutions?

Fetching data from database through logstash and adding into Elasticsearch.  
document\_id is combination of key + some additional information.  
logstash is running every few minutes to fetch latest created/modified data based timestamp.  
In my scenario, we have history table for every table. So, when record has been deleted from main table, it will be added into history table with DELETE action.  
I have 4-5 indexes for different purposes.  
Now, I want to update/remove this related record from Elasticsearch indexes.  
What are the best resolutions?

My thoughts

1. are there any facility using logstash or any other plugin through which I can fetch those deleted records and delete from Elasticsearch?
2. is it good to implement cron job using spring-boot to deleted records from Elasticsearch?

---

<div class="post-metadata">

**Author:** ![mverbeek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mverbeek/32/101709_2.png) [@mverbeek](https://discuss.elastic.co/u/mverbeek)\
**Post date:** [December 30, 2019, 2:18pm UTC](https://discuss.elastic.co/t/want-to-delete-documents-from-elasticsearch-which-are-deleted-from-database/213334/2 "2019-12-30T14:18:58Z")

</div>

You can delete specified records from Elastic with the '[Delete by query API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html)'.

You could put this into a cron job but it would not be the most clean solution.

---

<div class="post-metadata">

**Author:** ![milankamboya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/milankamboya/32/47041_2.png) [@milankamboya](https://discuss.elastic.co/u/milankamboya)\
**Post date:** [December 31, 2019, 6:26am UTC](https://discuss.elastic.co/t/want-to-delete-documents-from-elasticsearch-which-are-deleted-from-database/213334/3 "2019-12-31T06:26:11Z")

</div>

Is this direct? or using logstash?  
Any other approach?

---

<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:** [December 31, 2019, 8:08am UTC](https://discuss.elastic.co/t/want-to-delete-documents-from-elasticsearch-which-are-deleted-from-database/213334/4 "2019-12-31T08:08:00Z")

</div>

Not exactly the answer but I shared most of my thoughts regarding database sync there: [http://david.pilato.fr/blog/2015/05/09/advanced-search-for-your-legacy-application/](http://david.pilato.fr/blog/2015/05/09/advanced-search-for-your-legacy-application/)

Basically, I'd recommend modifying the application layer if possible and send data to elasticsearch in the same "transaction" as you are sending your data to the database.

---

<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:** [January 28, 2020, 8:08am UTC](https://discuss.elastic.co/t/want-to-delete-documents-from-elasticsearch-which-are-deleted-from-database/213334/5 "2020-01-28T08:08:00Z")

</div>

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