Skip to content

Instantly share code, notes, and snippets.

@jaygaha
Created March 11, 2025 08:05
Show Gist options
  • Select an option

  • Save jaygaha/d8e2e1961ad2cf02746628b647faa4d1 to your computer and use it in GitHub Desktop.

Select an option

Save jaygaha/d8e2e1961ad2cf02746628b647faa4d1 to your computer and use it in GitHub Desktop.
Expire inactive Redis keys that haven't been accessed for over given time, ensuring efficient memory management and improved performance.

Remove Unused Idle Redis Items:

If an item lacks a TTL setting and is not in use, it will persist in the storage indefinitely unless deleted.

Run this command to delete stale items inside redis-cli:

eval "for _,k in ipairs(redis.call('keys', '*')) do if redis.call('object', 'idletime', k) > {SECONDS} then redis.call('expire', k, 1) end end" 0

Note: {SECONDS} adjusts the time that the object is being idle.

The provided Redis command is a Lua script that performs the following actions:

  1. redis.call('keys', '*') lists all keys in the Redis database
  2. redis.call('object', 'idletime', k) for each key, it checks the idle time.
  3. If found unused for a given timeframe, it deletes it from the storage.
  4. The end 0 command indicates that the script doesn’t require any additional keys to be passed as arguments.

This script is useful for cleaning up inactive keys in a Redis database. It removes keys that haven’t been accessed for a specified duration, effectively managing memory usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment