In Spring, you implement cache eviction with the @CacheEvict annotation on a method that changes data: it removes one entry (by key) or every entry (allEntries = true) from a named cache. Pair it with @Cacheable for reads and @CachePut when you want to refresh an entry instead of removing it.
Eviction keeps cached data consistent with the database. Without it, a cache returns stale values after updates and deletes. The examples target Spring Framework 6 and Spring Boot 3.
Cache annotations only work when caching is enabled. Add the starter and the annotation:
With Caffeine on the classpath, Spring Boot auto-configures a CaffeineCacheManager. Without any provider it falls back to a simple ConcurrentMapCacheManager, which never expires entries on its own.
The following service caches products by id and changes data in other methods:
delete removes a single key after the method returns successfully. deleteAll empties the whole products cache. update uses @CachePut, which always runs the method and then stores the result, so the next read gets fresh data without a cache miss.
| Attribute | Purpose |
|---|---|
cacheNames / value | Cache or caches to evict from. |
key | SpEL expression for the entry to remove, for example #id or #product.id. |
keyGenerator | Custom key generator, used instead of key. |
allEntries | Remove every entry in the cache. |
beforeInvocation | Evict before the method runs instead of after it succeeds. |
condition | Evict only when the SpEL expression is true (evaluated before invocation). |
By default eviction happens after the method returns. If the method throws, nothing is evicted. Set beforeInvocation = true when the entry must disappear even if the method fails, for example when a failure leaves the data in an unknown state.
Use @Caching to combine operations, or list several names in one annotation:
Evicting a list cache by one key like this only works if the list key matches the category exactly. If list results are built from many filters, it is usually safer to evict the whole list cache with allEntries = true.
Sometimes annotations are not enough, for example when a message arrives from another service. Inject CacheManager and clear caches directly:
@Scheduled needs @EnableScheduling on a configuration class. For time-based removal, prefer a time-to-live on the cache itself (see below) over a cron job that wipes everything.
Caffeine can evict by age and size, so entries leave on their own:
Combine this with @CacheEvict on writes: the annotation handles explicit changes, and the time limit is your safety net if a code path forgets to evict.
@CacheEvict method from another method in the same class bypasses the proxy and nothing is evicted. Move the method into another bean.@CacheEvict must match the key used by @Cacheable. If one uses #id and the other uses the whole object, eviction silently misses.@TransactionalEventListener(phase = AFTER_COMMIT) handler or use a transaction-aware cache manager.@CacheEvict removes entries, so the next read reloads from the source. @CachePut runs the method and writes the result into the cache, so the next read hits the cache with new data.
Not by default. Eviction runs after a successful return unless you set beforeInvocation = true.
Use @CacheEvict(cacheNames = "name", allEntries = true) or call cacheManager.getCache("name").clear().
Yes. Use condition with a SpEL expression, for example condition = "#product.active".
Use @Cacheable for reads, @CacheEvict for deletes and invalidating writes, and @CachePut to refresh entries. Match keys exactly, avoid self-invocation, and add a time-to-live as a safety net. In clustered deployments, use a shared cache or invalidation events so every node sees evictions.