Last active
March 12, 2025 13:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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