programing

Redis 목록의 모든 항목 삭제

nasanasas 2020. 10. 29. 08:14
반응형

Redis 목록의 모든 항목 삭제


Redis에 LIST 데이터 유형이 있다고 가정합니다. 모든 항목을 어떻게 삭제합니까? 나는 이것을 이미 시도했다 :

LTRIM key 0 0
LTRIM key -1 0

둘 다 첫 번째 요소를 남깁니다. 이렇게하면 모든 요소가 남습니다.

LTRIM key 0 -1

목록을 완전히 비우는 별도의 명령이 표시되지 않습니다.


키를 삭제하면 모든 항목이 지워집니다. 목록이 전혀없는 것은 항목이없는 것과 유사합니다. Redis는 존재하지 않는 키에 액세스하려고 할 때 예외를 발생시키지 않습니다.

DEL key

다음은 몇 가지 콘솔 로그입니다.

redis> 키 *
(빈 목록 또는 세트)
redis> LPUSH 이름 John
(정수) 1
redis> LPUSH 이름 Mary
(정수) 2
redis> LPUSH 이름 Alice
(정수) 3
redis> LLEN 이름
(정수) 3
redis> LRANGE 이름 0 2
1) "앨리스"
2) "메리"
3) "존"
redis> DEL 이름
(정수) 1
redis> LLEN 이름
(정수) 0
redis> LRANGE 이름 0 2
(빈 목록 또는 세트)

당신은 모든 값을 제거 할 수 있습니다 목록 경우에만 목록이 하나 개 이상의 값을 가지고

LTRIM key -1 0

예:

redis 127.0.0.1:6379> RPUSH mylist four 1 3 1
(integer) 4
redis 127.0.0.1:6379> KEYS *
1) "newKey"
2) "firstHash"
3) "secondList"
4) "test4"
5) "firstList"
6) "mylist"
redis 127.0.0.1:6379> LTRIM mylist -1 0
OK
redis 127.0.0.1:6379> LRANGE mylist 0 -1
(empty list or set)
redis 127.0.0.1:6379> KEYS *
1) "newKey"
2) "firstHash"
3) "secondList"
4) "test4"
5) "firstList"

그러나 목록에 하나의 값만있는 경우 다음을 사용해야합니다.

DEL key 

LTRIM을 사용하십시오 . ... Aaaand 마법 여기가 사용하는 것입니다 start 보다 크다 end .

LTRIM key 99 0

그리고 ... 붐! 전체 요소 목록이 있습니다. 그냥 * 똥 * 바로 눈앞에 !! 잔여 물이 남아 있지 않으며 질문이 전혀 없습니다.

참고 : 이렇게하면 여기에 설명 된대로 키가 "제거"됩니다 ( 에서 와 같이 삭제됨 ) .

... start가 목록의 끝보다 크거나 start> end이면 결과는 빈 목록이됩니다 (키가 제거됨).


Am answering on my phone so can't actually format the answer well.

This might be a late response but am sticking it here just in case someone still needs that functionality.

Short answer

ltrim mylist 0 - (n+1) where mylist is key and n is length of mylist.

Long answer

The way ltrim works is that, it takes two indices and return the elements that fall between them inclusive of the indices.

Ltrim list startIndex endIndex

Example assuming we have a redis list with key mylist containing 10 entries:

ltrim mylist 0 5 will trim the list to elements starting from index 0 through to index 5. And discard those that fall outside that range.

Fortunately redis list operations support negative indexing which proves extremely useful in some situations. Typically when you don't know the length of the list.

-1 refers to last element, - 2 penultimate element, etc. And (-n) is the first element.

Out of range indexes are not harmful. If the end index is greater than length of the list, redis treats it as equal to last index.

This is why ltrim mylist 0, -(n +1) clear the list. It does so because (-n) is equivalent to index 0. Adding 1 to it leaves no element within that range since that will be before the first element.


I tried this and it works for me. Just change myList for your list name and execute it redis-cli KEYS "myList:*" | xargs redis-cli DEL


the accepted answer is wrong. suppose i have

redis 127.0.0.1:6379> KEYS * 
1) "newKey" 
2) "firstHash" 
3) "secondList" 
4) "test4" 
5) "firstList" 
6) "mylist" 

to use ahmed's example, which is actually correct. now, if i do:

DEL 'test4'

i end up with:

1) "newKey" 
2) "firstHash" 
3) "secondList" 
4) "firstList" 
5) "mylist"` 

so, i did not remove all entries from the 'test4' list, i removed test4 itself. not the same thing. not at all. i have a little app where the list keys are hashes computed from several data(well, aren't they all?), those lists sometimes are cleared, but the semantics of a empty list and a non-existent list are very different. so, no, i do not want to DEL 'myhashes', i want just to remove all entries.

beware, oh ye who wanders here.

참고URL : https://stackoverflow.com/questions/9828160/delete-all-entries-in-a-redis-list

반응형