<!-- review: finished -->

<a id="external-cache-purge"></a>

# Cache Purge

Allows clearing the cache content.

<a id="installation-5"></a>

## Installation

To [install](https://en.angie.software//angie/docs/installation/index.md#install-packages) the module, use one of the following packages:

- Angie: `angie-module-cache-purge`
- Angie PRO: `angie-pro-module-cache-purge`

<a id="loading-the-module-4"></a>

## Loading the Module

Loading the module in the `main{}` context:

```nginx
load_module modules/ngx_http_cache_purge_module.so;
```

<a id="configuration-example-81"></a>

## Configuration Example

```nginx
log_format test_cache '[$time_local] "$request" '
                      '$status $body_bytes_sent rt="$request_time" '
                      'ucs="$upstream_cache_status" us="$upstream_status" '
                      'ubr=$upstream_bytes_received';

proxy_cache_path /var/cache/angie/cache keys_zone=cache_zone:10m;

server {
    listen 80 default_server;

    location / {
        access_log /var/log/angie/cache-access.log test_cache;
        proxy_pass http://127.0.0.1:8080;

        proxy_cache cache_zone;
        proxy_cache_valid 10m;
        proxy_cache_key $uri$is_args$args;
        proxy_cache_purge PURGE from 127.0.0.1;
    }
}
```

<a id="preparing-for-demonstration-1"></a>

## Preparing for Demonstration

Requesting a file from the server:

```console
$ curl -s -o testf1.txt http://127.0.0.1/storage/testf1.txt
```

Checking the cache (MISS — file not found in cache):

```console
[05/Mar/2025:17:44:24 +0300] "GET /storage/testf1.txt HTTP/1.1" 200 519 rt="0.001" ucs="MISS" us="200" ubr=752
```

Re-requesting (HIT — file served from cache):

```console
$ curl -s -o testf1.txt http://127.0.0.1/storage/testf1.txt
```

```console
[05/Mar/2025:17:46:02 +0300] "GET /storage/testf1.txt HTTP/1.1" 200 519 rt="0.000" ucs="HIT" us="-" ubr=-
```

<a id="clearing-the-cache"></a>

## Clearing the Cache

```console
$ curl -X PURGE http://127.0.0.1/storage/*

<html><head><title>Successful purge</title></head><body bgcolor="white"><center><h1>Successful purge</h1><p>Key : /storage/*</p></center></body></html>
```

Requesting a file after cache clearing (MISS — file loaded again):

```console
$ curl -s -o testf1.txt http://127.0.0.1/storage/testf1.txt
```

```console
[05/Mar/2025:17:52:05 +0300] "GET /storage/testf1.txt HTTP/1.1" 200 519 rt="0.002" ucs="MISS" us="200" ubr=752
```

<a id="configuration-example-for-a-separate-cache-clearing-location"></a>

## Configuration Example for a Separate Cache Clearing Location

```nginx
proxy_cache_path /var/cache/angie/cache keys_zone=cache_zone:10m;

map $uri $wpurgeuri {
    "~/purge(?<wpurge>/.*)" $wpurge;
    default $uri;
}

server {
    listen 80 default_server;

    location / {
        access_log /var/log/angie/cache-access.log test_cache;
        proxy_pass http://127.0.0.1:8080;

        proxy_cache cache_zone;
        proxy_cache_valid 10m;
        proxy_cache_key $uri$is_args$args;
    }

    location /purge {
        allow 127.0.0.1;
        deny all;
        proxy_cache_purge cache_zone $wpurgeuri$is_args$args;
    }
}
```

Request for clearing the cache in this case:

```console
$ curl -X PURGE http://127.0.0.1/purge/storage/*
```

<a id="additional-information-5"></a>

## Additional Information

Full documentation of directives and source code is available at:
[https://github.com/nginx-modules/ngx_cache_purge](https://github.com/nginx-modules/ngx_cache_purge)
