The @CacheEvict annotation in Spring removes entries from a cache when the annotated method runs. You use it on methods that change or delete data, so the cache does not keep serving stale values that @Cacheable stored earlier. It can remove one entry by key or empty an entire cache.
It is part of Spring's cache abstraction and works with any supported provider, for example Caffeine, Redis or Ehcache, as long as caching is enabled with @EnableCaching.
A cache that is filled by @Cacheable and never cleaned eventually disagrees with the database. After an update or delete, readers still get the old value until the entry expires. Eviction closes that gap by removing the outdated entry at the moment the data changes, so the next read loads fresh data and caches it again.
After update(user) finishes successfully, the users entry for that id is removed. The next find(id) misses the cache, reads the row and stores the new value.
key is a SpEL expression and must produce the same key that the @Cacheable method used. A mismatch is the most common reason eviction seems to do nothing.
When the cached method used a custom keyGenerator, the evicting method must use the same generator or an equivalent key. If you rely on the default key and the method has a single argument, the argument itself is the key, so #id and the default are the same.
Set allEntries = true to empty the whole cache, for example after a bulk import or a configuration change.
Clearing everything is simple but expensive in a busy system, because every request afterwards misses the cache. Prefer key-based eviction when you know which entries changed.
By default eviction happens after the method returns successfully. If the method throws an exception, nothing is evicted. The beforeInvocation attribute changes that.
condition is a SpEL expression evaluated before the method runs. Eviction only happens when it is true.
| Attribute | Meaning |
|---|---|
value or cacheNames | Cache or caches to evict from |
key | SpEL expression for the entry to remove |
keyGenerator | Custom KeyGenerator bean, used when key is empty |
cacheManager or cacheResolver | Selects the cache manager or resolver |
condition | Evict only if the expression is true |
allEntries | Remove every entry in the cache |
beforeInvocation | Evict before the method runs instead of after |
Note that @CacheEvict has no unless attribute, because #result is not available for an operation that may run before the call.
Use @Caching to combine operations, or give several names in cacheNames.
@TransactionalEventListener(phase = AFTER_COMMIT).String key when the cache used a Long does not match.@CacheEvict removes entries and the next read repopulates them. @CachePut always runs the method and stores its result in the cache immediately.
Not by default. With the default beforeInvocation = false, the eviction happens only after the method completes normally. Set beforeInvocation = true to evict regardless.
Yes. Put @CacheEvict(allEntries = true) on a method annotated with @Scheduled, or rely on provider-level expiry such as a time to live.
Check that @EnableCaching is active, that the call goes through the Spring proxy, and that the eviction key equals the key used when the value was cached.
@CacheEvict keeps Spring caches consistent by removing a single entry or all entries when data changes. Match the key to the one used by @Cacheable, choose allEntries and beforeInvocation deliberately, and watch for self-invocation, transaction timing and multi-node caches.