Change record status: 
Project: 
Introduced in branch: 
11.2.x
Introduced in version: 
11.2.0
Description: 

It is possible to delete a cache and it is possible to invalidate a cache. If you delete it, the deleted cache items cease to exist. That means the item cannot be rendered from the cache under any circumstances.

Whereas if you invalidate data you do not destroy it; you mark it as stale, or, one might say, 'past its sell by date.' When you mark a cached item as invalid, the mark can in fact be ignored by setting $allow_invalid to TRUE.

It is like an item left on the supermarket shelf that is marked 'past its sell by date' but that is still available for purchase. Whereas deletion is equivalent to incinerating the item.

The default database implementation of cache invalidation handles it by marking all cache items as expiring in the past. Alternative cache implementation like Redis and Memcache need to separately track delete all vs invalidate all. Redis does that with an additional cache tag per bin. This makes this relatively expensive for cache bins/items that otherwise do not use cache tags.

invalidateAll() has no known use case compared to deleteAll() and is deprecated.

Note: Instead of invalidating/deleting the render cache bin, it is recommended to invalidate the deleted cache tag, as that also covers additional cache bins such as page cache and dynamic page cache.

Before:

// Invalidate a whole cache bin.
\Drupal::cache('menu')->invalidateAll();

After:


// Delete everything in a whole cache bin.
\Drupal::cache('menu')->deleteAll();

// Invalidate all cached rendered elements, including page and dynamic page caches.
\Drupal::service('cache_tags.invalidator')->invalidateTags(['rendered']);
Impacts: 
Module developers