# SDIFFSTORE

> Store the difference between the first set and the others in a destination key.

URL: https://docs.hotkv.com/commands/sdiffstore/

```
SDIFFSTORE destination key [key ...]
```

- Group: Set
- Complexity: O(N) where N is the total number of members in all given sets
- HotKV Cloud: all plans
- On-Premise: no license needed

`SDIFFSTORE` computes the same difference as [SDIFF](https://docs.hotkv.com/commands/sdiff/index.md) and
stores it as a set at `destination`, replacing whatever `destination` held
before (of any type, and without its time to live). If the result is empty,
`destination` is deleted. The source sets are read and the result written as
one atomic step, and `destination` can also be one of the source keys.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Success | Integer: the number of members in the stored set | Same |

`SDIFFSTORE` replies with a `WRONGTYPE` error if any source key holds a value
that is not a set. `destination` is not changed in that case.

## Examples

```
127.0.0.1:6379> SADD a "x" "y" "z"
(integer) 3
127.0.0.1:6379> SADD b "y"
(integer) 1
127.0.0.1:6379> SDIFFSTORE only_a a b
(integer) 2
127.0.0.1:6379> SMEMBERS only_a
1) "x"
2) "z"
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[SDIFF](https://docs.hotkv.com/commands/sdiff/index.md), [SINTERSTORE](https://docs.hotkv.com/commands/sinterstore/index.md), [SUNIONSTORE](https://docs.hotkv.com/commands/sunionstore/index.md),
[ZDIFFSTORE](https://docs.hotkv.com/commands/zdiffstore/index.md)