== Current Behavior
If a user has defined a `KEY_PREFIX`, it is likely they don't have access
to the full keyspace or don't want the Redis cache changing keys outside
the `KEY_PREFIX`. In these scenarios, the user either gets a permissions
error calling `cache.clear()` or deletes keys that are outside what the
Django cache manages.
== Expected Behavior
When `KEY_PREFIX` is defined, `cache.clear()` only deletes keys with that
prefix.
== Possible solutions
[https://stackoverflow.com/questions/21975228/redis-python-how-to-delete-
all-keys-according-to-a-specific-pattern-in-python/45153525#45153525 This
StackOverflow answer] benchmarks a few different ways to delete all keys
with a specific prefix.
`django-redis` has a `delete_pattern` method https://github.com/jazzband
/django-redis#scan--delete-keys-in-bulk
--
Ticket URL: <https://code.djangoproject.com/ticket/35039>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* type: Bug => New feature
* resolution: => wontfix
Comment:
This behavior is consistent with other backends and is
[https://docs.djangoproject.com/en/stable/topics/cache/#django.core.cache.cache.clear
documented] so it's not a bug:
> ''"Be careful with this; clear() will remove **everything** from the
cache, not just the keys set by your application"''
We agreed in #33012 to keep the builtin Redis backend minimal and
consistent with other backends. It looks like you should use `django-
redis` for your use case.
--
Ticket URL: <https://code.djangoproject.com/ticket/35039#comment:1>