In Django, how to clear all the memcached keys and values?
Asked Answered
G

3

23

I don't want to restart the memcached server!

Gamez answered 25/10, 2009 at 5:11 Comment(2)
for what task it can be useful?Peterman
...or if you memcached is serving several applications and you do not want to destroy everything at once!Maypole
I
42
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

Italianate answered 25/10, 2009 at 5:31 Comment(3)
For 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 cacheHoudan
I have CACHE_MIDDLEWARE_ALIAS pointing to a nondefault memcached cache but the command still tries to clear default and fails with AttributeError: 'FileBasedCache' object has no attribute '_cache'Epicanthus
These days, you can use from django.core.cache import cache; cache.clear()Thurlow
F
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]
Formosa answered 8/6, 2017 at 9:3 Comment(0)
S
1

An easiest and fastest way:

echo flush_all > /dev/tcp/localhost/11211
Serrato answered 29/4, 2020 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.