Skip to main content
0 reactions

How to Delete a Key Only If Its Value Matches in Valkey (DELEX)

Featured image for How to Delete a Key Only If Its Value Matches in Valkey, showing DELEX with the wrong owner replying 0, with the right owner replying 1, and GET job:owner replying nil.
Valkey DELEX.

DELEX deletes a key in Valkey® only if its value matches the one you give, so a worker can remove only what it owns. It’s new in Valkey 9.2. On 9.0 and 9.1, DELIFEQ does the matching part. This guide uses a job owned by worker-1.

Type the commands at the valkey-cli prompt. To open it, run valkey-cli in a terminal.

Note: Every command and output on this page is real, run on Valkey 9.2 on an Apple silicon Mac with macOS 26.

Syntax

SyntaxPlain text
DELEX key IFEQ value
DELEX key IFNE value
DELIFEQ key value
  • key: the key to delete. It must hold a string.
  • IFEQ value: delete only if the key holds exactly this value.
  • IFNE value: delete only if it holds anything else.
  • DELIFEQ key value: the same as DELEX key IFEQ value.

Both reply 1 when the key was deleted and 0 when it wasn’t. From the DELEX (opens in a new tab) and DELIFEQ (opens in a new tab) documentation.

Prerequisites

  • Valkey 9.2 or later for DELEX. DELIFEQ in Step 5 needs 9.0 or later.

Step 1: Save the Owner

127.0.0.1:6379>Plain text
SET job:owner "worker-1"
OutputPlain text
OK

Step 2: A Wrong Value Deletes Nothing

127.0.0.1:6379>Plain text
DELEX job:owner IFEQ "worker-2"
OutputPlain text
(integer) 0
127.0.0.1:6379>Plain text
GET job:owner
OutputPlain text
"worker-1"

0: the key holds worker-1, not worker-2, so it stays.

Step 3: The Right Value Deletes the Key

127.0.0.1:6379>Plain text
DELEX job:owner IFEQ "worker-1"
OutputPlain text
(integer) 1
127.0.0.1:6379>Plain text
GET job:owner
OutputPlain text
(nil)

Step 4: Delete Unless It Matches with IFNE

127.0.0.1:6379>Plain text
SET mode "test"
OutputPlain text
OK
127.0.0.1:6379>Plain text
DELEX mode IFNE "live"
OutputPlain text
(integer) 1

mode held test, not live, so IFNE deleted it.

Step 5: Do the Same on Valkey 9.0 and 9.1 with DELIFEQ

127.0.0.1:6379>Plain text
SET job:owner "worker-1"
OutputPlain text
OK
127.0.0.1:6379>Plain text
DELIFEQ job:owner "worker-1"
OutputPlain text
(integer) 1
valkey-cli session where DELEX with the wrong owner replies 0, with the right owner replies 1, DELEX with IFNE deletes mode, and DELIFEQ deletes job:owner.
Deleting a key only when its value matches.

Clean Up

Nothing to delete: the steps above deleted both keys.

Common Mistakes

MistakeWhat happensDo this instead
Reading the value with GET, then deleting with DELAnother client can change the key between the two commands, and you delete its valueUse DELEX … IFEQ, which checks and deletes in one step
Using DELEX on a key that isn’t a stringValkey replies with an errorUse it only on string keys

Troubleshooting

ProblemCauseFix
(error) ERR unknown command 'DELEX', with args beginning with: 'job:owner' 'IFEQ' 'worker-1'Your Valkey is older than 9.2Use DELIFEQ, or upgrade to Valkey 9.2 or later

FAQ

Why not GET the value and then DEL the key?

Another client could change the key between your two commands. The DELIFEQ documentation (opens in a new tab) calls the single-step check-and-delete the safe way to release a lock.

What does DELEX reply when the key doesn’t exist?

0, the same as when the value doesn’t match.

Conclusion

DELEX key IFEQ value deletes a key only when it holds that value, and IFNE only when it holds something else. On Valkey 9.0 and 9.1, use DELIFEQ.

Additional Resources

Valkey® is a registered trademark of LF Projects, LLC. Redis® is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with the Valkey project, LF Projects or Redis Ltd., and isn’t endorsed, supported or certified by them.

0 comments