In Django, how to clear all the memcached keys and values?
12,777
Solution 1
from django.core.cache import cache
cache._cache.flush_all()
Also see this ticket, it has a patch (that I haven't tested) to flush any type of cache backend: http://code.djangoproject.com/ticket/11503
Solution 2
And an one-liner from console:
echo "from django.core.cache import cache; cache._cache.flush_all()" | ./manage.py shell [--settings=myapp.settings_live]
Solution 3
An easiest and fastest way:
echo flush_all > /dev/tcp/localhost/11211
Author by
TIMEX
Updated on June 03, 2022Comments
-
TIMEX about 2 years
I don't want to restart the memcached server!
-
kayluhb almost 12 yearsFor anyone who needs it, the cache lib is located in django.core.cache. So to run the command above, you need to import cache from django.core.cache import cache
-
int_ua over 9 yearsI have
CACHE_MIDDLEWARE_ALIAS
pointing to a nondefaultmemcached
cache but the command still tries to cleardefault
and fails withAttributeError: 'FileBasedCache' object has no attribute '_cache'
-
Shadow almost 9 yearsThese days, you can use
from django.core.cache import cache; cache.clear()