title: How to actively delete a state in Flink after a fixed amount of time?
body: <p>In a Flink job, I want to delete state in memory 24 hours after it is constructed. I checked this <a href="https://www.da-platform.com/blog/state-ttl-for-apache-flink-how-to-limit-the-lifetime-of-state" rel="nofollow noreferrer">post</a> and set up state time to live(ttl), but as it mentioned in this article, state removal is lazy/passive, which may lead to memory leak. </p>
  
  <p>For example, after 23 hours 57 minutes I got the last message for key ('USA', 'Male', 2018), and after that no more message comes for this key. Then I won't be able to invoke the function and the ttl of the state for this key ('USA', 'Male', 2018), then it will be kept in memory forever.</p>
  
  <p>This article mentioned using a timer:
  <code>The idea is to register a timer with the TTL per state value and access. When the timer elapses, the state can be cleared if no other state access happened since the timer was registered.</code>
  but I am not able to figure out how to do that.</p>
  
  <p>I'm thinking about using <code>ProcessFunction</code> which has a <code>onTimer()</code> method. My plan is to register a <code>ProcessingTimeTimer</code> in its <code>open()</code> method, and delete the state in <code>onTimer()</code>, but I don't know if this timer is also passively triggered, meaning it won't fire if there no invocation of the <code>ProcessFunction</code> even after 24 hours.</p>