Node Redis: Get Keys Without TTL (Detailed Guide w/ Code Examples)

Use Case(s)

You may need to get all keys from a Redis store that do not have an expiration set (TTL). This can be useful for auditing, debugging or implementing specific business logic.

Code Examples

In Node.js, using the node-redis package, there isn't a direct way to fetch keys without TTL. Instead, you'll need to fetch all keys and then filter out keys with a TTL. Here's how to do it:

const redis = require('redis'); const client = redis.createClient(); client.keys('*', (err, keys) => { if (err) throw err; keys.forEach((key) => { client.ttl(key, (err, ttl) => { if (err) throw err; // If the TTL is -1, the key does not have an expiration if (ttl === -1) { console.log(key); } }); }); });

This script gets all keys ('*') and checks each key's TTL. Keys without an expiration have a TTL of -1.

Best Practices

Avoid running the script frequently on production systems as the 'keys' command can be resource-intensive, especially if you have a large number of keys.

Common Mistakes

One common mistake is not checking for errors when calling client.keys or client.ttl. Always include error handling to ensure you can catch and handle any potential issues.


Q: Can I get keys without TTL directly? A: No Redis does not provide a direct way to fetch keys without TTL. You need to fetch all keys first and then check each key's TTL.

Q: What does a TTL of -1 mean? A: In Redis, a TTL (Time To Live) of -1 means the key does not have an expiration time set.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.