-
Notifications
You must be signed in to change notification settings - Fork 954
Delete keys matching pattern #2501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: unstable
Are you sure you want to change the base?
Conversation
3a1dfb2 to
c9938f1
Compare
Signed-off-by: Satheesha Chattenahalli Hanume Gowda <[email protected]>
c9938f1 to
0c77880
Compare
|
If we add an UNLINK option to SCAN, it turns SCAN into a write command, to it needs the WRITE command flag. It means SCAN can't be executed on readonly replicas. This would be a breaking change. I think it would be better to introduce a new command like SCANDEL with the same options as SCAN. We may also want to think about this feature can be combined with a cluster-wide scan as discussed in #33. If you want to delete many keys in this way, the replies will be large. Maybe some users would just want to returns the number of deleted keys instead of the list of keys? Regarding your custom Lua script: If you iterate until cursor == 0 in the Lua script, it will hang the server. Slow scripts can cause cluster failovers and other unintended effects. It's better to let the script behave like your suggested scan command, i.e. return a new cursor each time. The loop should be on the client side, possible with some small sleep between each call if you don't want to keep the server too busy. We could add a correct and well-tested Lua script to our docs, so users don't need to invent this by themselves. We could also add this feature to valkey-cli even without a new server command. valkey-cli can use Lua. |
I totally agree, and it was my concern as well .
This is an interesting ask, we would be interested to look into it.
we concur with this approach. If we have consensus, will re-purpose this PR to introduce new command |
|
Decision: Wait to see if there is more traction. We'll discuss this topic in Amsterdam as well. |
|
Will add my thoughts from the weekly meeting: I think people started using restricted namespace / keyspace for their application as we shaped that as the future with cluster mode (single db) and ACL rules supporting keyspace restriction. However, we never provided any support to clean up the keyspace similar to Instead of adding a new command for
|
|
@hpatro I agree, we can nudge people to using databases, but there may be other reasons to delete keys matching a pattern. Now, we have optimizations for scanning a pattern that implies a single slot. We can scan only one slot in this case. I believe SCAN + delete is a very common use case. As @stockholmux mentioned in a meeting, people want to avoid Lua scripts because they're error prone. (Example: In the script posted above, in cluster mode, the UNLINK call with multiple keys can return a CROSSSLOT error, if SCAN returned keys from multiple slots.) I'm leaning towards accepting a SCANDEL command. I've created an issue for it, to decouple it from this implementation PR: |
Problem Statement
Valkey currently lacks a built-in, safe method for deleting keys matching a pattern. This fundamental gap forces developers into complex, error-prone workflows that compromise both performance and reliability.
Current Limitations
Developers must resort to suboptimal solutions that introduce significant complexity:
1. Multi-Step Client-Side Operations
Issues: Not atomic, prone to partial failures, requires external tooling
2. Custom Lua Scripting
Issues: Complex cursor management, memory pressure from collecting all keys, requires advanced Lua expertise, potential script timeout on large datasets
3. Manual Cursor Management
Developers must implement complex logic handling:
These workarounds lead to increased development time, maintenance burden, and potential data inconsistencies.
Proposed Solution
Enhance the
SCANcommand with an optionalUNLINKparameter that enables safe, atomic, non-blocking deletion of matching keys during iteration.Enhanced Command Syntax
Usage Examples
Basic Pattern Deletion
Server Command Examples
Alternatives you've considered:
SCANUNLINKorSCANDEL, which could lead to API fragmentation and duplicate/fragmented logic.UNLINKcommand to take pattern as an argument ex:UNLINK session:*, which could lead to blocks on large datasets, breaks UNLINK semanticsFuture Scope
If this PR is accepted , we have plan to add
UNLINKoptions to following commands as well