Purge
Azion Lib functions of the azion package that purge URLs, cache keys, and wildcard expressions from the cache through Azion API v4.
The azion/purge module is the Azion Lib library for Real-Time Purge. Its functions remove URLs, cache keys, and wildcard expressions from the cache through Azion API v4, so the next request fetches the current version from the origin. Each function takes a list of strings and returns a response envelope instead of throwing.
Install the package:
The azion package receives bug fixes only, and its maintenance ends in December 2026.
Every sample below is a TypeScript ES module with top-level await, run in Node.js. Types come in through import type, so the sample still loads after its type annotations are stripped.
Authentication
Each purge request carries your personal token. The functions take it from the AZION_TOKEN environment variable, and a createClient client takes it from its token field.
| Variable | Description |
|---|---|
AZION_TOKEN | Your Azion personal token. |
AZION_DEBUG | With true, turns on debug mode. |
To see where each Azion Lib package looks for these values, refer to How Azion Lib works.
Response envelope
Every function returns an AzionPurgeResponse object, { data?, error? }. On success, data holds an AzionPurge object: items, the list the purge received, and state. On failure, error holds { message, operation }, and operation is post purge.
Each function sends one request to Azion API v4. The API answers with the items and the layer it purged, and the envelope keeps only items and state:
| Function | API request | Argument |
|---|---|---|
| purgeURL | POST /v4/workspace/purge/url | A list of URLs |
| purgeCacheKey | POST /v4/workspace/purge/cachekey | A list of cache keys |
| purgeWildCard | POST /v4/workspace/purge/wildcard | A list of wildcard expressions |
The functions take no layer argument, and the API reports layer: 'cache' in its response. For the layers and the number of items each purge type accepts, refer to Real-Time Purge.
createClient
Creates a client that holds a token and exposes the three purge functions as methods.
| Parameter | Type | Required | Description |
|---|---|---|---|
token | string | No | Your Azion personal token. |
options | AzionClientOptions | No | Client options. |
Returns an AzionPurgeClient. Its methods take the same list as the matching function on this page, without options. To log the response the API returns, pass { debug: true } to a function, as the purgeURL sample does.
This sample creates a client and purges one URL with it:
Output:
purgeURL
Purges a list of URLs from the cache. Only the URLs in the list leave the cache.
| Parameter | Type | Required | Description |
|---|---|---|---|
url | string[] | Yes | The URLs to purge. |
options | AzionClientOptions | No | Request options. With debug: true, the function logs the response the API returns. |
Returns data as an AzionPurge object with the purged URLs in items. A URL whose host belongs to no domain of your account returns error; the message is in Errors.
This sample purges one URL in debug mode, so the output starts with the API response:
Output:
purgeCacheKey
Purges a list of cache keys from the cache. A cache key names one variation of a cached object.
| Parameter | Type | Required | Description |
|---|---|---|---|
cacheKey | string[] | Yes | The cache keys to purge, without a scheme. |
options | AzionClientOptions | No | Request options. With debug: true, the function logs the response the API returns. |
Returns data as an AzionPurge object with the purged keys in items. A key carries no scheme: the host/path form is accepted, and a key that starts with a scheme, such as http://, is refused with error. For the format of a key, refer to Cache keys.
This sample purges one cache key in debug mode:
Output:
purgeWildCard
Purges the cached objects that match a wildcard expression: a URL with an asterisk (*) in the path or in the query string.
| Parameter | Type | Required | Description |
|---|---|---|---|
wildcard | string[] | Yes | The wildcard expressions to purge. |
options | AzionClientOptions | No | Request options. With debug: true, the function logs the response the API returns. |
Returns data as an AzionPurge object with the expression in items. The type takes a list, and Real-Time Purge accepts one wildcard expression per request. For the expressions it accepts, refer to Wildcard purge.
This sample purges every cached object under one path prefix:
Output:
Errors
A refused purge returns error with the HTTP status only. The reason the API gives, such as The Content must be a valid cachekey., does not reach the envelope. error.operation is post purge for every function.
| Message | Cause | What to do |
|---|---|---|
Error: HTTP error! Status: 400 - Bad Request | A cache key passed to purgeCacheKey starts with a scheme, such as http://. | Pass the key without the scheme, in the host/path form. |
Error: HTTP error! Status: 400 - Bad Request | The host of a URL passed to purgeURL belongs to no domain of your account. | Purge a URL on a domain of your account. |
For the error codes the API returns for each purge type, refer to Real-Time Purge.
Types
The azion/purge module exports these types. Import them with import type.
AzionPurgeClient
The object createClient returns, with one method per purge type. Each method takes one list.
| Method | Argument | Returns |
|---|---|---|
purgeURL | urls: string[] | Promise<AzionPurgeResponse<AzionPurge>> |
purgeCacheKey | cacheKeys: string[] | Promise<AzionPurgeResponse<AzionPurge>> |
purgeWildCard | wildcards: string[] | Promise<AzionPurgeResponse<AzionPurge>> |
CreateAzionPurgeClient
The type of createClient.
AzionClientOptions
Request options that every function takes in options.
| Property | Type | Required | Description |
|---|---|---|---|
debug | boolean | No | Logs the response the API returns, when passed to a function. |
force | boolean | No | — |
AzionPurgeResponse
The object each purge function returns. Response envelope describes the success and failure forms.
| Property | Type | Required | Description |
|---|---|---|---|
data | T | No | The purge result, set on success. |
error | { message: string; operation: string } | No | The failure message and the name of the failed operation, set on failure. |
AzionPurge
The result of a purge.
| Property | Type | Required | Description |
|---|---|---|---|
state | 'executed' | 'pending' | Yes | The state of the purge request. |
items | string[] | Yes | The URLs, cache keys, or wildcard expressions the purge received. |