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

  1. DISCARD

Parameter Explanations

This command does not take any parameters.

Return Values

  • OK: If the transaction was successfully discarded.
  • Error: If DISCARD is called outside of a MULTI/EXEC context.

Code Examples

  1. dragonfly> MULTI
  2. OK
  3. dragonfly> SET key1 "value1"
  4. QUEUED
  5. dragonfly> SET key2 "value2"
  6. QUEUED
  7. dragonfly> DISCARD
  8. OK
  9. dragonfly> GET key1
  10. (nil)
  11. dragonfly> GET key2
  12. (nil)

Best Practices

  • Use DISCARD only 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 DISCARD outside of a MULTI/EXEC block 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.