Elasticsearch - Deleting the previous versions of a document

148 views Asked by At

I have a versioned index in Elasticsearch where I'm storing the document whenever a modification is made to it. After running it for some time I've realised that storing all the versions of a document is taking a lot of disk space. I don't have any version field instead I use a date field in order to see the latest version and the one previous to it.

I just want to keep the last two versions. But I'm unable to figure out a way to delete the previous versions.
Is there any way to do it?

1

There are 1 answers

4
Christophe Quintard On

Periodically run a deletion of all documents ordered by date, from the latest to the oldest, starting from 2 ?

POST /INDEX/_delete_by_query?from=2&sort=DATE:desc

where INDEX is your index, and DATE the name of your date field.

Note: the 'from' parameter has been removed in Elasticsearch 8.x.