0
0
Redisquery~3 mins

DISCARD to abort in Redis

Choose your learning style9 modes available
Introduction
DISCARD lets you stop a group of commands you started with MULTI, so nothing changes in the database.
You started a transaction but want to cancel it before running any commands.
You realize a mistake in your commands and want to avoid applying wrong changes.
You want to reset the transaction state and start fresh.
You want to safely exit a transaction block without making changes.
Syntax
Redis
DISCARD
Use DISCARD only after starting a transaction with MULTI.
DISCARD cancels all queued commands and ends the transaction.
Examples
Starts a transaction, queues a SET command, then cancels everything so no change happens.
Redis
MULTI
SET key1 value1
DISCARD
Queues an increment command but then aborts the transaction, so counter stays the same.
Redis
MULTI
INCR counter
DISCARD
Sample Program
Starts a transaction, queues setting and getting 'name', then cancels all queued commands. The final GET shows the original value or nil if none.
Redis
MULTI
SET name "Alice"
GET name
DISCARD
GET name
OutputSuccess
Important Notes
DISCARD only works if you have an active transaction started by MULTI.
After DISCARD, the transaction is closed and you can start a new one.
If you do not call EXEC or DISCARD after MULTI, the connection stays in transaction mode.
Summary
DISCARD cancels all commands queued in a transaction.
Use DISCARD to safely abort changes before they happen.
Always pair MULTI with EXEC or DISCARD to end the transaction.