Back to results for “Distributed Locks with Redis”
Redis › Transactions

How do you cancel a queued Redis transaction before it executes, and what command is used for this?

Beginner

Answer

You use the DISCARD command, which cancels the current transaction and clears all commands that were queued after MULTI was called, returning the connection to its normal state without executing any of the queued commands.
MULTI
SET temp_key 'value'
DISCARD
Real-world example An application starts building a transaction but decides midway through, based on some application logic, that the operation should not proceed, so it calls DISCARD to safely cancel everything queued so far.

Common follow-ups: Does DISCARD affect commands that were already executed outside the transaction?;What is the difference between DISCARD and simply not calling EXEC?