0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

redisで特定のデータを一括で削除、操作をするスクリプト

Posted at

redisで有効期限が設定されていないセッションデータを一括削除するスクリプト

今回は有効期限が設定されていないデータを削除するscriptを作りました。

シンプルに検索して、eachして、ifでチェックして該当すれば削除を行ってます。

session-delete.lua
redis.call('select', '1') --[[ DBの数字を選んでください ]]
local expresion = 'session*' --[[ 精査対象を正規表現で捉えてください ]]

local delete_count = 0
for _,session_key_id in ipairs(redis.call('keys', expresion))
do
  if redis.call('TTL', session_key_id) == -1 then --[[ 有効期限が -1 (未設定)のものを削除します ]]
    redis.call('DEL', session_key_id)
    delete_count = delete_count + 1
  end
end
return 'deleted:'..delete_count

実行方法は簡単

bash
redis-cli eval "$(cat search.lua)" 0

docker環境ならこんな感じで

bash
dc exec imdb redis-cli eval "$(cat search.lua)" 0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?