In case all the bitop command operands are empty (or none exist), maxlen will not be updated, and therefore remains zero, which lead to deletion of the target key - https://github.com/redis/redis/blob/unstable/src/bitops.c#L763. This is an unexpected behaviour which is not documented in redis.io (see https://redis.io/commands/bitop).

Comment From: oranagra

@fadidahanna it seems correct behaviour to me. Maybe I'm missing something.

In redis a missing key is considered an empty set, empty list, etc. eg. when you pop the last element from a list, it deletes the key.

If you perform an SUNIONSTORE and such, it either replaces the dest key, or deletes it if the result is an empty set. As you can see in the code it looks very intended.

So much like a union on empty / missing keys deletes the dest, an AND on a missing set of keys (or a list of 0 input keys) would delete the dest. (which in the case of bitops, is the same as a string with infinite number of 0 bits).

The only case where that would be invalid is for NOT (a NOT on an infinite 0 bits should result in a string of infinite number of 1 bits), but luckily that one is blocked at the top (NOT must take exactly 2 arguments)

Let me know if that explains it, or is there something I'm missing.

Comment From: fadidahanna

Summary of a discussion with @oranagra on DM - In case of NOT operation on an empty/non existing key: the operand is consider to be a string with 0 bits length, and therefore the output is 0 bits length too and the key indeed need to be deleted.