# Keep last N records

**URL:** <https://community.influxdata.com/t/keep-last-n-records/34664>\
**Category:** InfluxQL\
**Tags:** query\
**Created:** [June 21, 2024, 10:05am UTC](https://community.influxdata.com/t/keep-last-n-records/34664 "2024-06-21T10:05:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shashwat\_007](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/shashwat_007/32/15807_2.png) [@shashwat\_007](https://community.influxdata.com/u/shashwat_007)\
**Post date:** [June 21, 2024, 10:05am UTC](https://community.influxdata.com/t/keep-last-n-records/34664/1 "2024-06-21T10:05:43Z")

</div>

Hi,  
I want to keep last n records in a influxdb measurement and wanted to delete the remaning. This ordering is based on timestamp. How to do so?

---

<div class="post-metadata">

**Author:** ![shashwat\_007](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/shashwat_007/32/15807_2.png) [@shashwat\_007](https://community.influxdata.com/u/shashwat_007)\
**Post date:** [June 21, 2024, 10:07am UTC](https://community.influxdata.com/t/keep-last-n-records/34664/2 "2024-06-21T10:07:11Z")

</div>

I am using influxQL to do so.

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [June 24, 2024, 2:34pm UTC](https://community.influxdata.com/t/keep-last-n-records/34664/3 "2024-06-24T14:34:35Z")

</div>

@shashwat_007 From the post tag, I assume you’re using InfluxDB 1.x. InfluxDB 1.x doesn’t support these kinds of deletes. You can delete an entire database, measurement, or specific series.

---

<div class="post-metadata">

**Author:** ![PabloFraire](https://avatars.discourse-cdn.com/v4/letter/p/fbc32d/32.png) [@PabloFraire](https://community.influxdata.com/u/PabloFraire)\
**Post date:** [June 26, 2024, 12:12pm UTC](https://community.influxdata.com/t/keep-last-n-records/34664/4 "2024-06-26T12:12:22Z")

</div>

Hi @scott!

Does it means that in InfluxDB2 is there a way to set the retention policy to number of points (records) instead of time? Or only that you can delete specific points?  
I like the retention concept in influx.  
I am trying to build an inventory/monitoring system based on the telegraf snmp plugin.  
For inventory info I would like to have i.e. 10 points no matter how old.  
For monitoring data the aging policy is great.

Regards!  
Pablo

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [June 28, 2024, 2:23pm UTC](https://community.influxdata.com/t/keep-last-n-records/34664/5 "2024-06-28T14:23:11Z")

</div>

Retention periods are only time-based, not count based. When a timestamp of a point exceeds the retention period of a bucket, that point is marked for deletion and will be removed the next time data gets compacted.

You may need to store the inventory-specific information outside of InfluxDB. If you’re using a SQL database, you can use [Flux to query the data with SQL](https://docs.influxdata.com/flux/v0/query-data/sql/) and join it with your data stored in InfluxDB at query time.

---

<div class="post-metadata">

**Author:** ![PabloFraire](https://avatars.discourse-cdn.com/v4/letter/p/fbc32d/32.png) [@PabloFraire](https://community.influxdata.com/u/PabloFraire)\
**Post date:** [June 28, 2024, 2:40pm UTC](https://community.influxdata.com/t/keep-last-n-records/34664/6 "2024-06-28T14:40:02Z")

</div>

Thanks!  
Yes, I am considering that option, using Telegraf to send inventory data to a MySQL and monitoring data to InfluxDB.
