The docs of SETEX state that the SETEX command is equivalent to executing the following commands: SET mykey value EXPIRE mykey seconds And EXPIRE when called with a non-positive timeout result in the key being deleted rather than expired. But if SETEX key -1 123 is called then the following message is returned in Redis: ERR invalid expire time in setex. Is this the expected behavior or should SETEX key -1 123 delete the key if it already existed according to the doc in https://redis.io/commands/setex.

Comment From: oranagra

first, i'd like to state that SETEX is deprecated, it's an alias for SET EX in any case, calling EXPIRE with a negative number could have some purpose (deleting the key, but generating an expiration key-space notification, rather than DEL). but what would be the point of doing SET EX with negative number? it'll override (delete) the pre-existing key, then put a new one and immediately delete (expire) it?

note this was last changed here: https://github.com/redis/redis/pull/8287#issuecomment-781479957 (see https://github.com/redis/redis/pull/9839 too).

@itamarhaber WDYT?

Comment From: itamarhaber

I can't think of a use case where one would intentionally set and negatively expire, so I'm comfortable with what we have now.

Personally, I detest the way EXPIRE behaves when provided with a negative TTL - I find it non-intuitive and confusing (given that TTL returns a negative value for persistent keys). However, that isn't going to change.

So perhaps the way to resolve this is by amending the (now obsoleted) SETEX page with a note about this exception. Makes sense?

Comment From: daniel-house

I like the idea of adding a note to the SETEX page.