question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Efficient way to delete keys by pattern

See original GitHub issue

I’m working on a big project and in some places, I have to delete keys by a pattern, for example, I want to delete all the keys that start with filterProducts

I made a Google search and found some methods to do so like, for example, the method in this article

But I don’t know what is the efficient one for a big project, could you please help me?

  • Version: 2.8.0
  • Platform: Linux Mint 18.2(Built on Ubuntu 16.04)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

24reactions
MohammedAl-Mahdawicommented, Mar 5, 2018

For anyone that looking for the final result that we until now reached to and believe it is the most efficient way is the following:

//key example "prefix*"
function getKeysByPattern(key) {
    return new Promise((resolve, reject) => {
        var stream = redis.scanStream({
            // only returns keys following the pattern of "key"
            match: key,
            // returns approximately 100 elements per call
            count: 100
        });

        var keys = [];
        stream.on('data', function (resultKeys) {
            // `resultKeys` is an array of strings representing key names
            for (var i = 0; i < resultKeys.length; i++) {
                keys.push(resultKeys[i]);
            }
        });
        stream.on('end', function () {
            resolve(keys)
        });
    })
}

//key example "prefix*"
function deleteKeysByPattern(key) {
    var stream = redis.scanStream({
        // only returns keys following the pattern of "key"
        match: key,
        // returns approximately 100 elements per call
        count: 100
    });

    var keys = [];
    stream.on('data', function (resultKeys) {
        // `resultKeys` is an array of strings representing key names
        for (var i = 0; i < resultKeys.length; i++) {
            keys.push(resultKeys[i]);
        }
    });
    stream.on('end', function () {
        redis.unlink(keys)
    });
}

//key example "prefix*"
function batchDeletionKeysByPattern(key) {
    var stream = redis.scanStream({
        // only returns keys following the pattern of "key"
        match: key,
        // returns approximately 100 elements per call
        count: 100
    });

    stream.on('data', function (resultKeys) {
        if (resultKeys.length) {
            redis.unlink(resultKeys);
        }
    });
}

The code above is for ioredis. Please if you are someone that knows a better way of doing so please don’t hesitate to share.

6reactions
MohammedAl-Mahdawicommented, Mar 5, 2018

@dirkbonhomme Thank you so much for your help, I really appreciate that.

I followed your advice and used ioredis and created the following functions, could you please just tell me for big projects is the following the most efficient way for doing so or not:

//key example "prefix*"
function getKeysByPattern(key) {
    return new Promise((resolve, reject) => {
        var stream = redis.scanStream({
            // only returns keys following the pattern of "key"
            match: key,
            // returns approximately 100 elements per call
            count: 100
        });

        var keys = [];
        stream.on('data', function (resultKeys) {
            // `resultKeys` is an array of strings representing key names
            for (var i = 0; i < resultKeys.length; i++) {
                keys.push(resultKeys[i]);
            }
        });
        stream.on('end', function () {
            resolve(keys)
        });
    })
}

//key example "prefix*"
function deleteKeysByPattern(key) {
    var stream = redis.scanStream({
        // only returns keys following the pattern of "key"
        match: key,
        // returns approximately 100 elements per call
        count: 100
    });

    var keys = [];
    stream.on('data', function (resultKeys) {
        // `resultKeys` is an array of strings representing key names
        for (var i = 0; i < resultKeys.length; i++) {
            keys.push(resultKeys[i]);
        }
    });
    stream.on('end', function () {
        redis.unlink(keys)
    });
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Find and Delete multiple keys matching by a pattern in Redis
Use scanstream to find and pipeline to delete keys matching pattern. ... there is no way to delete all keys matching a pattern...
Read more >
How to Delete Keys Matching a Pattern in Redis | RDBTools
This command will delete all keys matching users:* If you are in redis 4.0 or above, you can use the unlink command instead...
Read more >
How to atomically delete keys matching a pattern using Redis
The following Bash script demonstrates deletion of keys by pattern: #!/bin/bash if [ $# -ne 3 ] then echo "Delete keys from Redis...
Read more >
Ways to delete multiple keys from Redis cache. - Medium
Redis cache key can be deleted in two ways. With Keys Command. Keys command will scan all the keys present in the Redis...
Read more >
Redis delete all keys with prefix - Easy way to do it - Bobcares
If we want to delete a bunch of keys that match a common prefix, we delete them by pattern. At Bobcares, we often...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found