# Document expiry in FoundationDB

**URL:** <https://forums.foundationdb.org/t/document-expiry-in-foundationdb/4164>\
**Category:** Running FoundationDB\
**Created:** [September 21, 2023, 12:09am UTC](https://forums.foundationdb.org/t/document-expiry-in-foundationdb/4164 "2023-09-21T00:09:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bharathrani1980](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@bharathrani1980](https://forums.foundationdb.org/u/bharathrani1980)\
**Post date:** [September 21, 2023, 12:09am UTC](https://forums.foundationdb.org/t/document-expiry-in-foundationdb/4164/1 "2023-09-21T00:09:57Z")

</div>

Hi,

I am trying to find a way to setup TTL in foundationdb to auto expire the documents. Can someone please help if you have implemented this feature. Appreciate your help.

Thanks

---

<div class="post-metadata">

**Author:** ![jwr](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/jwr/32/302_2.png) [@jwr](https://forums.foundationdb.org/u/jwr)\
**Post date:** [September 26, 2023, 9:30am UTC](https://forums.foundationdb.org/t/document-expiry-in-foundationdb/4164/2 "2023-09-26T09:30:35Z")

</div>

FWIW, I also needed this, and I’m doing this manually.

---

<div class="post-metadata">

**Author:** ![Imperatorx](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@Imperatorx](https://forums.foundationdb.org/u/Imperatorx)\
**Post date:** [September 26, 2023, 10:02am UTC](https://forums.foundationdb.org/t/document-expiry-in-foundationdb/4164/3 "2023-09-26T10:02:39Z")

</div>

I would do it this way:

- Include the expiry date in every document
- Create an index structure for the documents based on the expiry date: (expires\_at, document\_id) = empty byte array
- When reading a document, check if it has been expired, if it has, return null, but also delete the document (lazy cleanup)
- Periodically run a process that range reads document ids in batches from the index from 0 to current time and deletes those items (eager cleanup)

If you have a very large number of documents, instead of the index (expires\_at, document\_id) you can create a (logical\_partition, expires\_at, document\_id) so that frequent writes of the index will hit different storage nodes and also you can parallelize the background batch process.

---

<div class="post-metadata">

**Author:** ![hbs](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/hbs/32/1419_2.png) [@hbs](https://forums.foundationdb.org/u/hbs)\
**Post date:** [September 29, 2023, 3:03pm UTC](https://forums.foundationdb.org/t/document-expiry-in-foundationdb/4164/4 "2023-09-29T15:03:12Z")

</div>

Wouldn’t it be possible to add the possibility to delete data from a key range if said data has a version older than a threshold? This would make it easy to implement a TTL mechanism.

A scan could also benefit from the possibility to only read data within a given version range.
