# How to limit persistent context data created by the state-flow node

**URL:** <https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412>\
**Category:** General\
**Created:** [11 December 2020 21:13 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412 "2020-12-11T21:13:19Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [11 December 2020 21:13 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/1 "2020-12-11T21:13:19Z")

</div>

Thank you for this [guide!](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/75) My question: Is there a method to clear out records older than X, or when a file size is reached, or some other manner to help ensure that the stored file size does not grow too large? In my use of this, I am only wishing to store 48 hours of persistent data.

Any assistance greatly appreciated!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 December 2020 21:26 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/2 "2020-12-11T21:26:10Z")

</div>

What sort of data are you storing there?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 December 2020 22:27 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/3 "2020-12-11T22:27:11Z")

</div>

You have to do that yourself. How, depends on the structure of the data and how you are putting it into the variable.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [12 December 2020 14:52 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/4 "2020-12-12T14:52:34Z")

</div>

Great information (although still a bit over my head). My simple use of persistable storage is related to a single UI node (for now at least) of the ui\_statetrail node with the persistable storage checkbox chosen.

 ![persist](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bc3e40e0dee21e4832a030e4ac9db31ccce99830.png)

I am not aware of any additional settings within that contrib node but the data is currently being written to a json file in the node red context folder

![store](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/cac469350ff8f4e9a7e316a16615174b87caa1dd.png)

Within that file each entry does have a timestamp

![data](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/c/5c9d548b72facf12bcd7e5060562eb169d781c34.png)

With this description would you be able to help me get started to develop the function node within node-red to parse/delete records older than timestamp \< Now() - 48 hours?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 December 2020 15:48 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/5 "2020-12-12T15:48:10Z")

</div>

I haven't used that node, but I would have assumed that with the period set to 24 hours as you have it that it would only save 24 hours worth of data. Is that not what you are finding?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [12 December 2020 16:06 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/6 "2020-12-12T16:06:29Z")

</div>

I'm on the phone right now but let's see if I can explain.  
In the function node:  
The data is an array where every element has timestamp.  
You'll need to read that array from context.

`var Yourarray=context.get("Yourarray") || []`

Then do your stuff with that array  
And may be add new entry to array.  
Before storing the array back to context filter out all elements where timestamp is older than 48 hours. Note that all calculations are done in milliseconds.

```auto
var time = Yourarray[Yourarray.length - 1].timestamp - 172800000
Yourarray = Yourarray.filter(el => el.timestamp > time);

```

And now the array is ready to be stored back into context.

`context.set("Yourarray",Yourarray)`

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 December 2020 16:55 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/7 "2020-12-12T16:55:07Z")

</div>

> [@Tomadoggy](#):
>
> Great information (although still a bit over my head).

This whole write up was dealing with the persistence storage used by the base node-red nodes. How 'contrib' nodes store the data is up to them. But you will still have to deal with the data while NR is running and - most likely - build your own flow to remove things that are older than X numbeer of days.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [12 December 2020 16:58 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/8 "2020-12-12T16:58:23Z")

</div>

Thank you to all! This was the best thread I could find to help lead me to a solution, understanding that contrib nodes are their own animal, I wanted to increase my understanding to achieve my goal. I will take all this feedback and see if I can craft a solution.

BTW, the contrib node is storing more than the 24 hours of data for some reason. I will keep working.

Kind Regards,

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [12 December 2020 17:10 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/9 "2020-12-12T17:10:22Z")

</div>

Oh, you are talking about making the node not using the context in function. Then my previous reply is just for fun. But the logic is same. So not entirely pointless

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [12 December 2020 17:13 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/10 "2020-12-12T17:13:07Z")

</div>

> [@Tomadoggy](#):
>
> BTW, the contrib node is storing more than the 24 hours of data for some reason.

Is that about the state trail node? Bug you have found?

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [12 December 2020 17:14 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/11 "2020-12-12T17:14:22Z")

</div>

Not certain I would call it a state trail contrib node bug...Still investigating

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 December 2020 17:15 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/12 "2020-12-12T17:15:41Z")

</div>

@hotNipi is it expected that the stored data for the state trail node would keep increasing? I would have expected it only to store data for the configured period.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [12 December 2020 17:16 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/13 "2020-12-12T17:16:44Z")

</div>

Only for period for sure. Expecting bug report if not.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 December 2020 17:25 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/14 "2020-12-12T17:25:48Z")

</div>

NOTE: I've move this conversation out of the `A guide to understanding ‘Persistent Context’` thread since is is a bit off topic.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [12 December 2020 17:28 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/15 "2020-12-12T17:28:11Z")

</div>

Understood...and thank you. I have built a separate small flow with another state trail contrib node to test the duration of the persistent storage. After some time I will review that new storage file and timestamps to determine the period for the stored data.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [14 December 2020 19:13 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/16 "2020-12-14T19:13:22Z")

</div>

Hello...just reporting back on the investigation of this contrib node storage. Even though the "timestamps" within the file are nonsensical, it does appear to be maintaining the data in a fashion consistent with the chosen duration within the node. No Bug! Sorry for the bother.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [14 December 2020 19:20 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/17 "2020-12-14T19:20:16Z")

</div>

State-trail node does everything what is possible to reduce amount of data to handle and store. In case of option combine similar states is used, the not needed timestamps are thrown away and never stored.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 February 2021 19:20 UTC](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412/18 "2021-02-12T19:20:17Z")

</div>

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