Skip to content

Instantly share code, notes, and snippets.

@mesutpiskin
Last active March 12, 2025 13:35
Show Gist options
  • Save mesutpiskin/a1aa7cc60ba9c654719b4cc107894097 to your computer and use it in GitHub Desktop.
Save mesutpiskin/a1aa7cc60ba9c654719b4cc107894097 to your computer and use it in GitHub Desktop.
Redis Delete Keys Matching a Search Pattern with Go Lang. Redis client https://github.com/go-redis/redis
package main
import (
"context"
"fmt"
"os"
"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
func main() {
redisClient := redis.NewClient(&redis.Options{
Addr: "IP:PORT",
Password: "PASSWORD",
DB: 0,
})
searchPattern := "MYKey*"
if len(os.Args) > 1 {
searchPattern = os.Args[1]
}
var foundedRecordCount int = 0
iter := redisClient.Scan(ctx, 0, searchPattern, 0).Iterator()
fmt.Printf("YOUR SEARCH PATTERN= %s\n", searchPattern)
for iter.Next(ctx) {
fmt.Printf("Deleted= %s\n", iter.Val())
redisClient.Del(ctx, iter.Val())
foundedRecordCount++
}
if err := iter.Err(); err != nil {
panic(err)
}
fmt.Printf("Deleted Count %d\n", foundedRecordCount)
redisClient.Close()
}
@rustymagnet3000
Copy link

rustymagnet3000 commented Mar 12, 2025

did you try this with a pipeline ?
maybe this is not supported as examples don't use it: https://github.com/redis/go-redis/blob/master/iterator_test.go

@mesutpiskin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment