w3resource

Redis Transactions: DISCARD

Redis DISCARD Command

Redis DISCARD command is used to flush the all previously queued commands in a transaction and restores the connection state to normal.

If WATCH was used, DISCARD unwatches all keys watched by the connection.

Here are some common uses and scenarios for the "DISCARD" command:

  • Abort Transaction: Cancels the transaction started with the MULTI command, discarding all queued commands.
  • Error Handling: Provides a mechanism to handle errors or unexpected conditions by aborting the transaction and preventing command execution.
  • Resource Management: Frees up resources by discarding commands that were queued but are no longer needed.
  • Safe Rollback: Ensures a safe rollback by aborting all operations queued in the transaction, preserving data integrity.

Syntax:

DISCARD

Available since

2.0.0.

Return Value

Simple string reply: always OK.

Return Value Type

String

Example:

127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> INCR test
QUEUED
127.0.0.1:6379> INCR test1
QUEUED
127.0.0.1:6379> DISCARD
OK

Previous: UNSUBSCRIBE
Next: EXEC



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-discard.php