delete redis keys NOT matching a pattern using Java












2















I want to delete redis keys that DO NOT match a particular pattern.
Redis data looks like this -



prefix9_key1
prefix9_key2
prefix11_key1
prefix11_key2
prefix2_key1
prefix2_key2


These prefix values are randomly generated numbers (example 1123, 3422, 9879).



I know what my current prefix is (let's say it is prefix11). But there is no way to know what were the earlier prefixes being used. I want to delete all keys that DO NOT use current prefix value (prefix2* and prefix9*).



I read many posts mentioning deleting keys matching a pattern. I want to delete keys that do not match a particular pattern.










share|improve this question



























    2















    I want to delete redis keys that DO NOT match a particular pattern.
    Redis data looks like this -



    prefix9_key1
    prefix9_key2
    prefix11_key1
    prefix11_key2
    prefix2_key1
    prefix2_key2


    These prefix values are randomly generated numbers (example 1123, 3422, 9879).



    I know what my current prefix is (let's say it is prefix11). But there is no way to know what were the earlier prefixes being used. I want to delete all keys that DO NOT use current prefix value (prefix2* and prefix9*).



    I read many posts mentioning deleting keys matching a pattern. I want to delete keys that do not match a particular pattern.










    share|improve this question

























      2












      2








      2








      I want to delete redis keys that DO NOT match a particular pattern.
      Redis data looks like this -



      prefix9_key1
      prefix9_key2
      prefix11_key1
      prefix11_key2
      prefix2_key1
      prefix2_key2


      These prefix values are randomly generated numbers (example 1123, 3422, 9879).



      I know what my current prefix is (let's say it is prefix11). But there is no way to know what were the earlier prefixes being used. I want to delete all keys that DO NOT use current prefix value (prefix2* and prefix9*).



      I read many posts mentioning deleting keys matching a pattern. I want to delete keys that do not match a particular pattern.










      share|improve this question














      I want to delete redis keys that DO NOT match a particular pattern.
      Redis data looks like this -



      prefix9_key1
      prefix9_key2
      prefix11_key1
      prefix11_key2
      prefix2_key1
      prefix2_key2


      These prefix values are randomly generated numbers (example 1123, 3422, 9879).



      I know what my current prefix is (let's say it is prefix11). But there is no way to know what were the earlier prefixes being used. I want to delete all keys that DO NOT use current prefix value (prefix2* and prefix9*).



      I read many posts mentioning deleting keys matching a pattern. I want to delete keys that do not match a particular pattern.







      redis






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 6:45









      Vibhav Singh RohillaVibhav Singh Rohilla

      334213




      334213
























          2 Answers
          2






          active

          oldest

          votes


















          0














          From the docs:




          h[^e]llo matches hallo, hbllo, ... but not hello




          But, the specific example you gave is a bit tricky. Here is how you can get the desired filtering:



          =>redis-cli keys *
          1) "pre1_234"
          2) "pre3_234"
          3) "pre11_asv"
          4) "pre2_234"

          =>redis-cli keys 'pre[^1]*' | redis-cli keys 'pre?[^1]*'
          1) "pre1_234"
          2) "pre3_234"
          3) "pre2_234"


          To me redis-cli keys 'pre[^1][^1]'* should have given the desired output. But it's not working. The pattern matcher short circuits at the first [^1] and hence pre1_234 is not part of the output. The vagaries of regex :)



          You can use lua script for atomic deletion in one call to Redis. See this.






          share|improve this answer

































            0














            You need a lua UDF for this (below example uses jedis as redis client),



            String DELETE_SCRIPT = "local keys = redis.call('keys', '%s')" +
            " for i,k in ipairs(keys) do" +
            " local res = redis.call('del', k)" +
            " end";


            Your pattern will be a string input to the method with the prefix like prefix[^11]*



            public void deleteOthers (String pattern) {
            String luaScript = String.format(DELETE_SCRIPT, pattern);
            jedis.eval(luaScript);
            }


            Call to this method would be something like,



            deleteOthers("prefix[^11]*");





            share|improve this answer
























            • prefix[^11]* will not work. see my answer above. Both prefix[^11]* and prefix[^1][^1]* will ignore keys like 'prefix1_key4'. Also, Redis follows glob-style pattern. In which, "[abc] matches one character given in the bracket" en.wikipedia.org/wiki/Glob_(programming)

              – rainhacker
              Jan 2 at 20:35













            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002298%2fdelete-redis-keys-not-matching-a-pattern-using-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            From the docs:




            h[^e]llo matches hallo, hbllo, ... but not hello




            But, the specific example you gave is a bit tricky. Here is how you can get the desired filtering:



            =>redis-cli keys *
            1) "pre1_234"
            2) "pre3_234"
            3) "pre11_asv"
            4) "pre2_234"

            =>redis-cli keys 'pre[^1]*' | redis-cli keys 'pre?[^1]*'
            1) "pre1_234"
            2) "pre3_234"
            3) "pre2_234"


            To me redis-cli keys 'pre[^1][^1]'* should have given the desired output. But it's not working. The pattern matcher short circuits at the first [^1] and hence pre1_234 is not part of the output. The vagaries of regex :)



            You can use lua script for atomic deletion in one call to Redis. See this.






            share|improve this answer






























              0














              From the docs:




              h[^e]llo matches hallo, hbllo, ... but not hello




              But, the specific example you gave is a bit tricky. Here is how you can get the desired filtering:



              =>redis-cli keys *
              1) "pre1_234"
              2) "pre3_234"
              3) "pre11_asv"
              4) "pre2_234"

              =>redis-cli keys 'pre[^1]*' | redis-cli keys 'pre?[^1]*'
              1) "pre1_234"
              2) "pre3_234"
              3) "pre2_234"


              To me redis-cli keys 'pre[^1][^1]'* should have given the desired output. But it's not working. The pattern matcher short circuits at the first [^1] and hence pre1_234 is not part of the output. The vagaries of regex :)



              You can use lua script for atomic deletion in one call to Redis. See this.






              share|improve this answer




























                0












                0








                0







                From the docs:




                h[^e]llo matches hallo, hbllo, ... but not hello




                But, the specific example you gave is a bit tricky. Here is how you can get the desired filtering:



                =>redis-cli keys *
                1) "pre1_234"
                2) "pre3_234"
                3) "pre11_asv"
                4) "pre2_234"

                =>redis-cli keys 'pre[^1]*' | redis-cli keys 'pre?[^1]*'
                1) "pre1_234"
                2) "pre3_234"
                3) "pre2_234"


                To me redis-cli keys 'pre[^1][^1]'* should have given the desired output. But it's not working. The pattern matcher short circuits at the first [^1] and hence pre1_234 is not part of the output. The vagaries of regex :)



                You can use lua script for atomic deletion in one call to Redis. See this.






                share|improve this answer















                From the docs:




                h[^e]llo matches hallo, hbllo, ... but not hello




                But, the specific example you gave is a bit tricky. Here is how you can get the desired filtering:



                =>redis-cli keys *
                1) "pre1_234"
                2) "pre3_234"
                3) "pre11_asv"
                4) "pre2_234"

                =>redis-cli keys 'pre[^1]*' | redis-cli keys 'pre?[^1]*'
                1) "pre1_234"
                2) "pre3_234"
                3) "pre2_234"


                To me redis-cli keys 'pre[^1][^1]'* should have given the desired output. But it's not working. The pattern matcher short circuits at the first [^1] and hence pre1_234 is not part of the output. The vagaries of regex :)



                You can use lua script for atomic deletion in one call to Redis. See this.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 2 at 16:32

























                answered Jan 2 at 16:25









                rainhackerrainhacker

                1638




                1638

























                    0














                    You need a lua UDF for this (below example uses jedis as redis client),



                    String DELETE_SCRIPT = "local keys = redis.call('keys', '%s')" +
                    " for i,k in ipairs(keys) do" +
                    " local res = redis.call('del', k)" +
                    " end";


                    Your pattern will be a string input to the method with the prefix like prefix[^11]*



                    public void deleteOthers (String pattern) {
                    String luaScript = String.format(DELETE_SCRIPT, pattern);
                    jedis.eval(luaScript);
                    }


                    Call to this method would be something like,



                    deleteOthers("prefix[^11]*");





                    share|improve this answer
























                    • prefix[^11]* will not work. see my answer above. Both prefix[^11]* and prefix[^1][^1]* will ignore keys like 'prefix1_key4'. Also, Redis follows glob-style pattern. In which, "[abc] matches one character given in the bracket" en.wikipedia.org/wiki/Glob_(programming)

                      – rainhacker
                      Jan 2 at 20:35


















                    0














                    You need a lua UDF for this (below example uses jedis as redis client),



                    String DELETE_SCRIPT = "local keys = redis.call('keys', '%s')" +
                    " for i,k in ipairs(keys) do" +
                    " local res = redis.call('del', k)" +
                    " end";


                    Your pattern will be a string input to the method with the prefix like prefix[^11]*



                    public void deleteOthers (String pattern) {
                    String luaScript = String.format(DELETE_SCRIPT, pattern);
                    jedis.eval(luaScript);
                    }


                    Call to this method would be something like,



                    deleteOthers("prefix[^11]*");





                    share|improve this answer
























                    • prefix[^11]* will not work. see my answer above. Both prefix[^11]* and prefix[^1][^1]* will ignore keys like 'prefix1_key4'. Also, Redis follows glob-style pattern. In which, "[abc] matches one character given in the bracket" en.wikipedia.org/wiki/Glob_(programming)

                      – rainhacker
                      Jan 2 at 20:35
















                    0












                    0








                    0







                    You need a lua UDF for this (below example uses jedis as redis client),



                    String DELETE_SCRIPT = "local keys = redis.call('keys', '%s')" +
                    " for i,k in ipairs(keys) do" +
                    " local res = redis.call('del', k)" +
                    " end";


                    Your pattern will be a string input to the method with the prefix like prefix[^11]*



                    public void deleteOthers (String pattern) {
                    String luaScript = String.format(DELETE_SCRIPT, pattern);
                    jedis.eval(luaScript);
                    }


                    Call to this method would be something like,



                    deleteOthers("prefix[^11]*");





                    share|improve this answer













                    You need a lua UDF for this (below example uses jedis as redis client),



                    String DELETE_SCRIPT = "local keys = redis.call('keys', '%s')" +
                    " for i,k in ipairs(keys) do" +
                    " local res = redis.call('del', k)" +
                    " end";


                    Your pattern will be a string input to the method with the prefix like prefix[^11]*



                    public void deleteOthers (String pattern) {
                    String luaScript = String.format(DELETE_SCRIPT, pattern);
                    jedis.eval(luaScript);
                    }


                    Call to this method would be something like,



                    deleteOthers("prefix[^11]*");






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 17:26









                    swayamrainaswayamraina

                    668711




                    668711













                    • prefix[^11]* will not work. see my answer above. Both prefix[^11]* and prefix[^1][^1]* will ignore keys like 'prefix1_key4'. Also, Redis follows glob-style pattern. In which, "[abc] matches one character given in the bracket" en.wikipedia.org/wiki/Glob_(programming)

                      – rainhacker
                      Jan 2 at 20:35





















                    • prefix[^11]* will not work. see my answer above. Both prefix[^11]* and prefix[^1][^1]* will ignore keys like 'prefix1_key4'. Also, Redis follows glob-style pattern. In which, "[abc] matches one character given in the bracket" en.wikipedia.org/wiki/Glob_(programming)

                      – rainhacker
                      Jan 2 at 20:35



















                    prefix[^11]* will not work. see my answer above. Both prefix[^11]* and prefix[^1][^1]* will ignore keys like 'prefix1_key4'. Also, Redis follows glob-style pattern. In which, "[abc] matches one character given in the bracket" en.wikipedia.org/wiki/Glob_(programming)

                    – rainhacker
                    Jan 2 at 20:35







                    prefix[^11]* will not work. see my answer above. Both prefix[^11]* and prefix[^1][^1]* will ignore keys like 'prefix1_key4'. Also, Redis follows glob-style pattern. In which, "[abc] matches one character given in the bracket" en.wikipedia.org/wiki/Glob_(programming)

                    – rainhacker
                    Jan 2 at 20:35




















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002298%2fdelete-redis-keys-not-matching-a-pattern-using-java%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas

                    Can't read property showImagePicker of undefined in react native iOS