I have key "test", the type is zset, I want to remove it.Its value is complex.
127.0.0.1:6379>zrange test 1 2
1) "\x80\x02}q\x01(U\x04bodyq\x02U\x00U\t_encodingq\x03U\x05utf-8q\x04U\acookiesq\x05}q\x06U\x04metaq\a}q\bU\x05depthq\tK\x01sU\aheadersq\n}q\x0bU\aRefererq\x0c]q\rU.http://guba.eastmoney.com/list,002273,f_1.htmlq\x0easU\x03urlq\x0fX0\x00\x00\x00http://guba.eastmoney.com/list,002273,f_334.htmlU\x0bdont_filterq\x10\x89U\bpriorityq\x11K\x00U\bcallbackq\x12U\x05parseq\x13U\x06methodq\x14U\x03GETq\x15U\aerrbackq\x16Nu."
2) "\x80\x02}q\x01(U\x04bodyq\x02U\x00U\t_encodingq\x03U\x05utf-8q\x04U\acookiesq\x05}q\x06U\x04metaq\a}q\bU\x05depthq\tK\x01sU\aheadersq\n}q\x0bU\aRefererq\x0c]q\rU.http://guba.eastmoney.com/list,002273,f_1.htmlq\x0easU\x03urlq\x0fX0\x00\x00\x00http://guba.eastmoney.com/list,002273,f_335.htmlU\x0bdont_filterq\x10\x89U\bpriorityq\x11K\x00U\bcallbackq\x12U\x05parseq\x13U\x06methodq\x14U\x03GETq\x15U\aerrbackq\x16Nu."
127.0.0.1:6379>zcard test
57232
There are so many values, I want to remove them all or some. How can I do this?
You can use DEL command to remove any key from redis database, regardless of the datatype associated with it.
DEL key [key ...]
Removes the specified keys. A key is ignored if it does not exist.
DEL will allow you to remove the key entirely: http://redis.io/commands/del.
ZREM will allow you to remove members from the set: http://redis.io/commands/zrem.
There are additional ZREM* commands that allow the removal of ranges of members - see ZREMRANGEBYLEX, ZREMRANGEBYRANK and ZREMRANGEBYSCORE
You use the "del" command to remove an entry from redis:
del test
See: http://redis.io/commands/del
Related
I'm wanting to a redis instance to be used for development purposes without any expirations in it. Is there way to remove the ttl from all the keys in the database?
The following command via redis-cli will loop through all the keys in the selected database and call the persist command which removes the ttl:
EVAL "for i, name in ipairs(redis.call('KEYS', '*')) do redis.call('persist', name); end" 0
When getting all the keys from Redis, like this:
redis.server.com:6379> keys *
1) "z13235jxby03knne1w1gucl5"
Instead of manually copying the long key to execute get z13235jxby03knne1w1gucl5, I'd like to run something like get $(1) (pseudo code) to get the value at position 1, as output by the keys command.
Is this possible, if not, is there any workaround to not have to manually copy paste?
Note, I don't want to solve this with a script, then I prefer just copy and paste it
off the top of my head, I'm not aware of a way from inside the cli.
But, Regardless of any performance implications, you can pipe the cli command lines together , but you have to do it from the shell
redis-cli --raw KEYS "*" | sed -n 1p | xargs redis-cli GET
where the 1 in :
sed -n 1p
is the line number (1-based index) inside KEYS output.
but still you need to do your validations; like making sure the index is withing the nuber of keys returned by the KEYS command all keys are of simple string type ; not sets, hash maps, etc...
I am trying to copy a specific set of keys from a Redis instance(v4.0.1) to another instance(v5.0.5). For this I am using MIGRATE command with COPY option:
migrate <ip-address of dest redis> 6379 "" 0 5000 COPY KEYS key_1 key_2 key_3
There are around 500 keys and it may be possible that the some keys are getting repeated and some don't even exist in the source.
I am getting the following error:
(error) ERR Target instance replied with error: BUSYKEY Target key
name already exists.
On the destination instance, I got some of the keys. Is this an error that can be ignored and can I be sure that all the existing keys are migrated?
The MIGRATE command internally uses DUMP to generate the serialized version of the key value, and RESTORE in order to synthesize the key in the target instance.
That error happens when on a RESTORE command the key already exists. If you want to replace existing keys on the targe instance, you should use the REPLACE optional argument. Please revise the full optional arguments here. https://redis.io/commands/migrate#options
I have a lot of analytics data that I'm adding to redis. I plan on incrementally moving the data out of redis and into my database.
I know I can use KEYS [the_key]:* to get all keys that match. For example, I can do that to get the following:
127.0.0.1:6379> KEYS c_Track:*
1) "c_Track:6c93a5c1-77e9-4c4a-9232-bf182713a02e"
2) "c_Track:2c9d99c2-af37-4de9-ac64-b48f339e97a9"
3) "c_Track:9e7fd190-86d9-4b4a-9a70-7bf4c7768eef"
4) "c_Track:7f2d2e98-7440-4fd7-a80a-2af309ab15a4"
Is there a recommended way to get these values easily? I can get the keys, but how can I get all the values as well? I can loop through the keys to get the values, but is there some one-shot method for doing this?
Also I know I shouldn't use keys, but this is just an example. Thanks
Thanks
Also I know I shouldn't use keys
So don't. Use SCAN instead.
is there some one-shot method for doing this?
No, not as a core Redis command, but given the need this is fairly simple to achieve with a server-side Lua script. For example, assuming that your values are strings, you could do something like the following:
local cursor = tonumber(ARGV[1])
local pattern = ARGV[2]
local scan = redis.call('SCAN', cursor, 'MATCH', pattern)
for i, v in ipairs(scan[2]) do
local val = redis.call('GET', v)
scan[2][i] = { v, val }
end
return scan
Assuming that this script is saved under "scan.lua", you can run it as follows:
$ redis-cli SET foo bar
OK
$ redis-cli SET baz qaz
OK
$ redis-cli --eval scan.lua , 0 "*"
1) "0"
2) 1) 1) "baz"
2) "qaz"
2) 1) "foo"
2) "bar"
To scan your entire keyspace, call the script with the returned cursor until it returns 0.
Notes:
1) If your keys are of different types, you should change the script accordingly (e.g. https://github.com/itamarhaber/redis-lua-scripts/blob/master/scanfetch.lua).
2) While this script goes against the common recommendation of generating key names inside a script, it is still safe to run as SCAN returns keys that are in the server's keyspace (whether single-instance or clustered).
My redis collection contains many keys
I want to be able to flush them all except all the keys that start with:
"configurations::"
is this possible?
You can do this
redis-cli KEYS "*" | grep -v "configurations::" | xargs redis-cli DEL
List all keys into the redis, remove from the list keys that contains "configurations::" and delete them from the redis
Edit
As #Sergio Tulentsev notice it keys is not for use in production. I used this python script to remove keys on prodution redis. I stoped replication from master to slave before call the script.
#!/usr/bin/env python
import redis
import time
pattern = "yourpattern*"
poolSlave = redis.ConnectionPool(host='yourslavehost', port=6379, db=0)
redisSlave = redis.Redis(connection_pool=poolSlave)
poolMaster = redis.ConnectionPool(host='yourmasterhost', port=6379, db=0)
redisMaster = redis.Redis(connection_pool=poolMaster)
cursor = '0'
while cursor != 0:
cursor, data = redisSlave.scan(cursor, pattern, 1000)
print "cursor: "+str(cursor)
for key in data:
redisMaster.delete(key)
print "delete key: "+key
# reduce call per second on production server
time.sleep(1)
The SCAN & DEL approach (as proposed by #khanou) is the best ad-hoc solution. Alternatively, you could keep an index of all your configurations:: key names with a Redis Set (simply SADD the key's name to it whenever you create a new configurations:: key). Once you have this set you can SSCAN it to get all the relevant key names more efficiently (don't forget to SREM from it whenever you DEL though).
Yes, it's possible. Enumerate all the keys, evaluate each one and delete if it fits the criteria for deletion.
There is no built-in redis command for this, if this is what you were asking.
It might be possible to cook up a Lua script that will do this (and it'll look to your app that it's a single command), but still it's the same approach under the hood.