How to delete multiple keys using python api and delete and zrem

2,004 views
Skip to first unread message

David Montgomery

unread,
Dec 25, 2012, 4:59:27 AM12/25/12
to redi...@googlegroups.com
Hi,

I am trying to delete multiple keys using delete and zrem.  Does that command support lists?  I did not work for me if so had to delete one by one.  I have millions of keys to delete.  What is the best way to do this other than 3am?

I can break down the keys to delete list into batches.

expire_list = [1,1]
rs.zrem('cookie_last_seen',expire_list))
rs.delete(expire_list)

Thanks


Dvir Volk

unread,
Dec 25, 2012, 6:06:56 AM12/25/12
to redi...@googlegroups.com
Both zrem and del support *args so you can pass lists to them:

keys = ('a', 'b', 'c')
r.zrem('my_key', *keys)
r.delete(*keys)

you probably want to do this in batches of a few thousands at a time, and you can use the python slicing syntax to sent just part of the list.
suppose keys is a list of millions of rows, we can do this:


step = 1000
for idx in xrange(0, len(keys), step):
   chunk = keys[idx:idx+step]
   r.zrem('my_key', *chunk)
   r.delete('*chunk)



--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.



--
Dvir Volk
Chief Architect, Everything.me

Amol Yadav

unread,
Nov 22, 2022, 9:28:10 AM11/22/22
to Redis DB
how do i delete after slicing key ex.  key="s_300_test" i want key[0:5] match all deleted 
Reply all
Reply to author
Forward
0 new messages