Sometimes, we need to delete all keys in all databases. This tutorial talks about how to delete keys in the Redis server database. It includes the following items.
- Delete everything in all databases in Redis Server
- Delete all keys in a current database?
- How to empty a redis database?
How to delete all keys in all database?
There are two commands used with the redis-cli command-line utility.
- FLUSHDB:
Delete all keys from the current connection database.
Syntax
FLUSHDB [Mode]
MODE is a modifier for flushing the database. ASYNC
| SYNC
.
ASYNC
: Deleting keys with asynchronous. It was added since Redis 4.0.0 version
SYNC
: Delete keys synchronously, Added since Redis 6.0.0 version
Here is an example to run with the redis-cli command
A:\Java\Redis>redis-cli flushdb
OK
The below command runs in CLI mode.
A:\Java\Redis>redis-cli
127.0.0.1:6379> flushdb
OK
- FLUSHALL
Delete everything including all keys from all databases in a server. It means wiping out entire data on the server.
Syntax
FLUSHALL [Mode]
MODE is a modifier for flushing all databases in a server. Values are ASYNC
| SYNC
.
ASYNC
: Deleting keys with asynchronous. It was added since Redis 4.0.0 version
SYNC
: Delete keys synchronously, Added since Redis 6.0.0 version
Here is an example to run with the redis-cli command
A:\Java\Redis>redis-cli flushall
OK
The below command runs in CLI mode.
A:\Java\Redis>redis-cli
127.0.0.1:6379> flushall
OK
Here is a complete example
- List all keys in the database using the
keys * command
, Initially empty list. - Also, the Database is empty
- Next add, two keys - id and name
- list the keys using the keys command
- Run, flushdb command, removes all keys in the current database
- Run, the keys command and show an empty list
Here is a sequence of steps
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> info keyspace
# Keyspace
127.0.0.1:6379> set id 11
OK
127.0.0.1:6379> keys *
1) "id"
127.0.0.1:6379> set name "john"
OK
127.0.0.1:6379> keys *
1) "name"
2) "id"
127.0.0.1:6379> keys *
1) "name"
2) "id"
127.0.0.1:6379> flushdb async
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>
Another way delete rdb and aof files.
- stop the Redis server
- Delete rdb and aof files
- Start Redis Server
- It starts with an empty and blank database.
## How to delete LUA scripts from Redis database?
It provides SCRIPT FLUSH Command to delete all LUA scripts from database instance.
It delete all scripts from scripts cache.
Syntax:
```markup
SCRIPT FLUSH [Mode]
MODE is a modifier for flushing all scripts in a server. Values are ASYNC
| SYNC
.
ASYNC
: Deleting scripts with asynchronous. It was added since Redis 4.0.0 version
SYNC
: Delete scripts synchronously, Added since Redis 6.0.0 version
Here is an example
127.0.0.1:6379> script flush
OK