Clearing the cache | laravel-responsecache | Spatie

 SPATIE

  Laravel Response Cache
=========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-responsecache](https://spatie.be/docs/laravel-responsecache/v8)  Basic-usage  Clearing the cache

 Version   v8

 Other versions for crawler [v8](https://spatie.be/docs/laravel-responsecache/v8)

- [ Introduction ](https://spatie.be/docs/laravel-responsecache/v8/introduction)
- [ Support us ](https://spatie.be/docs/laravel-responsecache/v8/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-responsecache/v8/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-responsecache/v8/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-responsecache/v8/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-responsecache/v8/changelog)
- [ About us ](https://spatie.be/docs/laravel-responsecache/v8/about-us)

Basic usage
-----------

- [ Caching responses ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/caching-responses)
- [ Flexible caching (SWR) ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/flexible-caching)
- [ Preventing caching ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/preventing-caching)
- [ Clearing the cache ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/clearing-the-cache)
- [ Using tags ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/using-tags)
- [ Events ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/events)

Advanced usage
--------------

- [ Custom cache profiles ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/custom-cache-profiles)
- [ Creating a replacer ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/creating-a-replacer)
- [ Customizing the serializer ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/customizing-the-serializer)
- [ Customizing the hasher ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/customizing-the-hasher)
- [ Configuration ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/configuration)

 Clearing the cache
==================

###  On this page

1. [ Clearing the entire cache ](#content-clearing-the-entire-cache)
2. [ Forgetting specific URIs ](#content-forgetting-specific-uris)
3. [ Forgetting a selection of cached items ](#content-forgetting-a-selection-of-cached-items)
4. [ Using model events ](#content-using-model-events)
5. [ Clearing tagged content ](#content-clearing-tagged-content)

Clearing the entire cache
-----------------------------------------------------------------------------------------------------------------------------------

You can clear the entire response cache programmatically.

```
use Spatie\ResponseCache\Facades\ResponseCache;

ResponseCache::clear();
```

You can also clear it using the artisan command.

```
php artisan responsecache:clear
```

Forgetting specific URIs
--------------------------------------------------------------------------------------------------------------------------------

You can forget specific URIs.

```
use Spatie\ResponseCache\Facades\ResponseCache;

// Forget one URI
ResponseCache::forget('/some-uri');

// Forget several URIs
ResponseCache::forget(['/some-uri', '/other-uri']);

// Or pass them as separate arguments
ResponseCache::forget('/some-uri', '/other-uri');
```

You can also forget a specific URI using the artisan command.

```
php artisan responsecache:clear --url=/some-uri
```

Forgetting a selection of cached items
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

For more control, you can use `selectCachedItems()` to specify exactly which cached items should be forgotten.

```
use Spatie\ResponseCache\Facades\ResponseCache;

// Forget all PUT responses for a URI
ResponseCache::selectCachedItems()
    ->withPutMethod()
    ->forUrls('/some-uri')
    ->forget();

// Forget multiple URIs
ResponseCache::selectCachedItems()
    ->forUrls('/some-uri', '/other-uri')
    ->forget();

// Forget with a specific cache name suffix (by default the suffix is the user ID or an empty string)
ResponseCache::selectCachedItems()
    ->usingSuffix('100')
    ->forUrls('/some-uri')
    ->forget();

// All options combined
ResponseCache::selectCachedItems()
    ->withPutMethod()
    ->withHeaders(['foo' => 'bar'])
    ->withCookies(['cookie1' => 'value'])
    ->withParameters(['param1' => 'value'])
    ->withRemoteAddress('127.0.0.1')
    ->usingSuffix('100')
    ->usingTags('tag1', 'tag2')
    ->forUrls('/some-uri', '/other-uri')
    ->forget();
```

Using model events
--------------------------------------------------------------------------------------------------------------

You can clear the cache whenever a model changes by using model events.

```
namespace App\Traits;

use Spatie\ResponseCache\Facades\ResponseCache;

trait ClearsResponseCache
{
    public static function bootClearsResponseCache(): void
    {
        self::created(fn () => ResponseCache::clear());
        self::updated(fn () => ResponseCache::clear());
        self::deleted(fn () => ResponseCache::clear());
    }
}
```

Clearing tagged content
-----------------------------------------------------------------------------------------------------------------------------

If you're using cache tags, you can clear only responses with specific tags.

```
use Spatie\ResponseCache\Facades\ResponseCache;

// Clear only responses tagged with 'posts'
ResponseCache::clear(['posts']);

// Clear responses tagged with both 'foo' and 'bar'
ResponseCache::clear(['foo', 'bar']);
```
