The @CacheEvict annotation removes one entry, or all entries, from a Spring cache when the annotated method is called. Its significance is correctness: it invalidates cached data at the moment the underlying data changes, so readers never keep getting an outdated value from @Cacheable.
If you have used @Cacheable to speed up reads, you almost always need @CacheEvict on the matching update and delete operations. This guide walks through common scenarios, one at a time, and shows how to test that eviction really works.
Spring's cache abstraction offers three main annotations that cover the life cycle of a cached value:
| Annotation | What it does |
|---|---|
@Cacheable | Returns the cached value when present, otherwise runs the method and stores the result. |
@CachePut | Always runs the method and stores the result. |
@CacheEvict | Removes entries from the cache. |
Enable the feature with @EnableCaching on a configuration class and add spring-boot-starter-cache in a Spring Boot project. The annotations are applied through proxies, so they work on public methods of Spring beans called from outside the bean.
The key is a SpEL expression. #user.id reads the id property of the user parameter, so only that user's entry is removed. The cache name and key must match the ones used by the @Cacheable read method.
With allEntries = true, the whole users cache is emptied. Do not set key together with it. This is useful after a bulk import or a nightly reload.
By default the entry is removed only after the method returns normally. If the method throws, the cache keeps the old entry. To remove it regardless:
Choose this when a stale entry would be harmful, for example a revoked permission that must never be served again.
condition is evaluated before the method runs; eviction happens only when it is true.
Listing several names evicts from each of them. For different keys per cache, combine annotations with @Caching(evict = { ... }).
A small test confirms both behaviours, the cache hit and the eviction:
The first two reads hit the repository once, and after updateUser the next read hits it again, proving the entry was evicted. In newer Spring Boot versions @MockBean is deprecated in favour of @MockitoBean; use whichever your version supports.
@EnableCaching, which makes every cache annotation a no-op.No. It only removes the cached copy. Your method must perform the database change itself.
Spring builds a default key from all method parameters, using SimpleKeyGenerator. It must match the key used when the value was cached.
After a successful return by default, or before the call when beforeInvocation = true.
The annotation is, but the cache provider decides the exact semantics. Caffeine and Redis handle concurrent access, though a read racing an update can briefly reload old data.
@CacheEvict invalidates cached entries when data changes. Use key for a single entry, allEntries for a full reset, beforeInvocation when the entry must go even on failure, and condition for selective eviction. Keep keys consistent with @Cacheable, avoid self-invocation, and test the behaviour with a mock repository.