DISCARD
Introduction and Use Case(s)
The DISCARD command in Redis is used to cancel a transaction started with MULTI. This command discards all the commands issued after the MULTI command and before the EXEC command. It is useful when you want to abort a transaction due to some conditional logic or error encountered during the transaction’s preparation.
Syntax
DISCARD
Parameter Explanations
This command does not take any parameters.
Return Values
- OK: If the transaction was successfully discarded.
- Error: If
DISCARDis called outside of aMULTI/EXECcontext.
Code Examples
dragonfly> MULTIOKdragonfly> SET key1 "value1"QUEUEDdragonfly> SET key2 "value2"QUEUEDdragonfly> DISCARDOKdragonfly> GET key1(nil)dragonfly> GET key2(nil)
Best Practices
- Use
DISCARDonly when necessary to ensure that transactions are managed appropriately. - Ensure that your application logic can handle cases where a transaction needs to be aborted.
Common Mistakes
- Attempting to use
DISCARDoutside of aMULTI/EXECblock will result in an error. - Forgetting to handle the state after a
DISCARD, leaving the system in an unintended state.
FAQs
What happens if I issue commands after DISCARD?
After issuing DISCARD, the transaction context is cleared, so subsequent commands are executed immediately rather than being queued for the transaction.
Can DISCARD be used inside Lua scripts?
No, DISCARD cannot be called from within Lua scripts. Lua scripts are atomic by nature and do not require transactional control via MULTI/DISCARD.