flush_all — Invalidate Everything
Logically invalidate all keys instantly (lazy deletion).
Code
# Invalidate ALL keys immediately (logically)
flush_all
# OK
# Delayed invalidation (after N seconds)
flush_all 60
# OK
# keys still readable for 60s, then ignored
# Verify
get user:42
# END <- returns nothing
# Python
from pymemcache.client.base import Client
c = Client(("localhost", 11211))
c.flush_all()
# WARNING: flush_all is global — affects ALL keys, not a namespace.
# There is no "delete pattern *" in Memcached.
# Namespacing trick: include a version prefix in keys, bump version to "flush"
# user:v3:42 <- bump v3 -> v4 to logically invalidate all user:* keys
# (old keys expire naturally or get evicted)
# Also: flush_all does NOT free memory immediately — items are
# removed lazily as they're requested or evicted. Restart the
# daemon to actually reclaim RSS.Explanation
flush_all marks every key as invalid instantly (it bumps an internal generation counter); the actual memory is reclaimed lazily on subsequent gets or via eviction. Use it for cache warmups, test resets, or emergency clears. There's no built-in pattern deletion or namespace invalidation — emulate it by versioning your key prefix ("user:v3:42" → "user:v4:42") and bumping the version. flush_all N delays invalidation by N seconds. Be cautious in shared deployments — it clears the whole instance, not just your app's keys.
More Memcached Snippets
set / get — Basic Key-Value
Store and retrieve values by key with expiration and flags.
add / replace / append / prepend
Conditional writes and in-place string concatenation.
cas / gets — Compare-And-Swap
Optimistic locking: update a key only if it hasn't changed since you read it.
stats — Server Metrics & Slabs
Inspect memory, hit rate, evictions and per-slab allocation.
Expiration, Eviction & TTL Strategy
Choose TTLs wisely and understand how Memcached evicts under pressure.
Text vs Binary Protocol & Consistent Hashing
Pick the right protocol and shard across servers without reshuffling.