2016-11-18 89 views
2

我在CentOS的安裝Redis的,我有這樣的Redis的多個按鍵,Redis的命令行刪除多個鍵

Product:<id>:<url> 

如何刪除所有Product:*:*與CLI?

Redis版本:3.2.4 [最新推測]

謝謝!

回答

2

沒有內置的命令。您必須使用SCAN命令獲取與該模式匹配的所有密鑰,然後使用DEL命令刪除這些密鑰。

// scan from cursor 0 to get the next cursor and keys 
SCAN 0 match Product:*:* 
// next_cursor, Product:x1:y1, Product:x2:y2, ... 
DEL Product:x1:y1 Product:x2:y2 ... 
// scan from the next cursor until it return 0 
SCAN next_cursor match Product:*:* 

另一種解決方案是使用HASH保存該模式鍵:

// set key value 
HSET Products Product:<id>:<url> value 
// remove a single key 
HDEL Products Product:<id>:<url> 
// remove all keys 
DEL Products 
+0

你能給我範例掃描然後刪除? –

+0

@JohnFG我更新了答案。有關'SCAN'的更多詳細信息,請查看文檔。 –

4

使用redis-cli工具,你可以做到以下幾點:

redis-cli --scan --pattern 'Product:*:*' | xargs redis-cli DEL