Redis batch delete KEY method
- 2020-05-09 19:36:33
- OfStack
There is an DEL instruction to delete a single Key in Redis, but there does not seem to be a batch Key instruction to delete Key, but we can do this with the xargs instruction of Linux.
redis-cli keys " * " | xargs redis-cli del
// if redis-cli Not set to system variable, need to be specified redis-cli Full path
// Such as: /opt/redis/redis-cli keys " * " | xargs /opt/redis/redis-cli del
To specify the Redis database access password, use the following command:
redis-cli -a password keys " * " | xargs redis-cli -a password del
To access a specific database in Redis, use the following command:
// The following command specifies the data sequence number as 0 , the default database
redis-cli -n 0 keys " * " | xargs redis-cli -n 0 del
Delete all Key
To delete all Key, you can use Redis's flushdb and flushall commands:
// Deletes all in the current database Key
flushdb
// Delete all databases key
flushall
Note: the keys directive can be used for fuzzy matching, but if Key contains Spaces, the match will not be available. No good solution has been found yet.