DELIFEQ(3valkey)

πŸ“¦ version 9.0.6, in fedora 44

✨ DELIFEQ ✨

okay bestie, so this is a page from the valkey manual (section 3valkey), and it's about a little command called delifeq that is genuinely so underrated, like it's giving ✨atomic lock release✨ and we're obsessed πŸ’…


πŸ’– NAME πŸ’–

delifeq β€” Delete key if value matches string. that's it, that's the whole vibe. no cap. πŸŽ€


🌸 SYNOPSIS 🌸

DELIFEQ key value

two things, that's all she needs. key and value. periodt. πŸ’œ


πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the tea πŸ“: delifeq deletes the key if its value matches the given string.

Where this hits different is distributed systems, fr fr. You use it to safely release locks β€” because it makes sure that the only person who can delete that lock is the actual owner of the lock (and the owner is identified by the value). nobody else gets to touch it, iykyk πŸ”₯

The rules, laid out plain:

  • If the key exists and the stored value is a string that exactly matches the provided value β†’ the key gets removed and 1 is returned. slay. πŸ’…
  • Otherwise β†’ the key is left completely unchanged and 0 is returned. no drama, no deletion. 🫢

πŸ’€ REPLY πŸ’€

You get one of these:

  • valkey-protocol(7) Integer reply: 1 if the key was deleted. ate. πŸ‘‘
  • valkey-protocol(7) Integer reply: 0 if the key was not deleted because it did not exist or the value was not equal to the provided value. 😭

⚑ COMPLEXITY ⚑

O(1)

constant time baby, she's fast, she's efficient, she's that girl 🌈


πŸ” ACL CATEGORIES πŸ”

@fast @string @write


πŸ“œ HISTORY πŸ“œ

  • Available since: 9.0.0

fresh out the oven, a newer icon ✨


🌟 EXAMPLES 🌟

Basic usage:

127.0.0.1:6379> SET mykey abc123
OK
127.0.0.1:6379> DELIFEQ mykey abc123
(integer) 1
127.0.0.1:6379> DELIFEQ mykey abc123
(integer) 0

first call: deleted, 1, iconic. second call: the key's already gone so it's a 0, we move on πŸ’…

Wrong value:

127.0.0.1:6379> SET mykey xyz789
OK
127.0.0.1:6379> DELIFEQ mykey abc123
(integer) 0

value said xyz789, you asked about abc123... not a match, key stays, 0. it's giving "wrong person, blocked" πŸ™ˆ

Wrong type:

127.0.0.1:6379> SADD mykey somevalue
(integer) 1
127.0.0.1:6379> DELIFEQ mykey somevalue
(error) WRONGTYPE Operation against a key holding the wrong kind of value

key is a set, not a string β†’ straight up error. we love a command that knows its boundaries πŸŽ€

🧚 Use case: Safe lock release 🧚

DELIFEQ enables an atomic check-and-delete operation, so you can safely release a lock only if the process still holds it. This dodges race conditions where a lock might expire and get claimed by another process before the original owner even tries to delete it. nightmare scenario, avoided. 😭

This pattern is commonly used as part of distributed locking systems like valkey-distlock(7) Redlock, which previously relied on Lua scripting to implement this logic. With DELIFEQ, such a use case becomes more robust and idiomatic. glow up fr πŸ’–

Instead of:

EVAL "if redis.call('GET',KEYS[1]) == ARGV[1] then return redis.call('DEL',KEYS[1]) else return 0 end" 1 mykey abc123

You can now use:

DELIFEQ mykey abc123

like?? look at the difference. one line. main character energy πŸ‘‘


πŸ‘€ SEE ALSO πŸ‘€

  • valkey-set(7) SET IFEQ β€” Sets the key only if it matches the given value. (the sibling, the counterpart, the other half of the duo 🫢)

πŸ’œ SEE ALSO (again, bc there's more) πŸ’œ

append(3valkey), decr(3valkey), decrby(3valkey), get(3valkey), getdel(3valkey), getex(3valkey), getrange(3valkey), getset(3valkey), incr(3valkey), incrby(3valkey), incrbyfloat(3valkey), lcs(3valkey), mget(3valkey), mset(3valkey), msetnx(3valkey), psetex(3valkey), set(3valkey), setex(3valkey), setnx(3valkey), setrange(3valkey), strlen(3valkey), substr(3valkey)

whole friend group right there, go say hi πŸ’…

and that's the page bestie!! πŸŽ€ version 9.0.0, dated 2025-10-21, from the DELIFEQ(3valkey) manual. go forth and release your locks safely, you absolute legend βœ¨πŸ’–πŸ¦‹

πŸ“¦ where this page comes from