The @CacheEvict annotation removes one entry, or every entry, from a Spring cache when the annotated method runs. You use it on methods that change data, such as update or delete operations, so that a cached copy created by @Cacheable does not keep serving stale values.
It belongs to Spring's cache abstraction, which Spring Boot enables with @EnableCaching and a cache provider such as Caffeine or Redis.
A cache is a copy. As soon as the source of truth changes, the copy can be wrong. Suppose findById is cached and then delete removes the row: without eviction, findById keeps returning the deleted record until it expires. @CacheEvict ties the removal of the copy to the method that changes the data.
After delete(7) completes, the entry with key 7 is gone from the products cache, so the next findById(7) goes to the database.
By default the eviction happens after the method returns successfully. If the method throws, the entry stays, which is usually what you want because the data did not change.
| Attribute | Purpose |
|---|---|
cacheNames / value | Which cache or caches to evict from |
key | SpEL expression for the entry to remove; defaults to the arguments |
keyGenerator | Custom key generator bean |
allEntries | If true, clears the whole cache and ignores key |
beforeInvocation | If true, evicts before the method runs, even if it then fails |
condition | SpEL; evict only when true |
cacheManager / cacheResolver | Select a specific manager or resolver |
Clearing all entries is right after a bulk change, but expensive on a busy system because every following request becomes a miss.
Use beforeInvocation = true when the method may fail but you still want the old entry gone.
When an update changes the data, you can either evict the key or replace it with fresh data using @CachePut:
When one method affects several caches, group the operations:
This evicts the single product and clears cached lists in one transaction-aware step. List-style caches are a common source of stale data because their key does not contain a product id.
@CacheEvict differs from the key in @Cacheable, so nothing is removed. Use the same expression, for example #id in both.@TransactionalEventListener.private method, which the proxy cannot intercept.@CacheEvict removes entries so the next read reloads them. @CachePut runs the method and writes the result into the cache immediately.
Not by default. The eviction runs after successful completion unless you set beforeInvocation = true.
Annotate a @Scheduled method with @CacheEvict(allEntries = true) and enable scheduling with @EnableScheduling. In most cases a TTL in the cache provider is simpler.
Use allEntries = true, or group several @CacheEvict annotations in @Caching. A single annotation evicts one key.
@CacheEvict removes stale entries from a Spring cache when data changes. Match its key to the @Cacheable key, use allEntries for bulk changes, consider beforeInvocation for failure cases, and watch for self-invocation and transaction timing. Pair it with @CachePut and @Caching for complete cache consistency.