Skip to content

Instantly share code, notes, and snippets.

@KavenTheriault
Created June 11, 2018 15:35
Show Gist options
  • Save KavenTheriault/b1c56c448198f8f4828a8d6b7ca40f91 to your computer and use it in GitHub Desktop.
Save KavenTheriault/b1c56c448198f8f4828a8d6b7ca40f91 to your computer and use it in GitHub Desktop.
Delete redis keys without ttl

The following operations need to be run on a unix system. You can use Bash on Windows is you have a Windows OS.

Install redis client

$ sudo apt-get install redis-tools

Create script file

Create the following bash file

#!/bin/bash

redis-cli -h 127.0.0.1 -p 6379 keys "test:*" > keys
cat keys | xargs -n 1 -L 1 redis-cli -h 127.0.0.1 -p 6379 ttl > ttl
paste -d " " keys ttl | grep .*-1$ | cut -d " " -f 1 > without_ttl
cat without_ttl | awk '{print "redis-cli -h 127.0.0.1 -p 6379 del "$1}' > redis.sh
  • You can change 127.0.0.1 with the desired server IP Address
  • You can change 6379 with the desired server Port number
  • You can change test:* with the desired key format to scan

Make the file executable

$ chmod +x your_file.sh

Executing scripts

  1. Execute your script file
  • This will create keys, ttl, without_ttl and redis.sh files
  1. You can review the files before continuing
  2. Execute the script redis.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment