The Cache API allows you to purge cached content and manage cache settings including cache reserve, tiered cache, and variants.
Purge cache
Purge cached content from Cloudflare’s cache. You can purge everything, specific URLs, files with custom cache keys, or content by tags, hostnames, or prefixes.
Purge everything
Removes all files from Cloudflare’s cache.
const response = await client.cache.purge({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
purge_everything: true,
});
Set to true to purge all cached content
Purge by URL
Granularly removes one or more files from Cloudflare’s cache by specifying URLs.
const response = await client.cache.purge({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
files: [
'http://www.example.com/css/styles.css',
'http://www.example.com/js/index.js',
],
});
An array of URLs to purge from cache
Purge with custom cache keys
Purge files with custom cache keys by including headers used to compute the cache key.
const response = await client.cache.purge({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
files: [
{
url: 'http://www.example.com/cat_picture.jpg',
headers: {
'CF-IPCountry': 'US',
'CF-Device-Type': 'desktop',
'Accept-Language': 'zh-CN',
},
},
{
url: 'http://www.example.com/dog_picture.jpg',
headers: {
'CF-IPCountry': 'EU',
'CF-Device-Type': 'mobile',
'Accept-Language': 'en-US',
},
},
],
});
An array of objects containing URL and headers for cache key matchingHeaders used to compute the cache key (CF-Device-Type, CF-IPCountry, Accept-Language, etc.)
Granularly removes files by specifying associated cache tags.
const response = await client.cache.purge({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
tags: ['a-cache-tag', 'another-cache-tag'],
});
An array of cache tags to purge
Purge by hostname
Purge all cached content for specified hostnames.
const response = await client.cache.purge({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
hosts: ['www.example.com', 'images.example.com'],
});
An array of hostnames to purge
Purge by prefix
Purge cached content by URL prefix.
const response = await client.cache.purge({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
prefixes: ['www.example.com/foo', 'images.example.com/bar/baz'],
});
An array of URL prefixes to purge
The purge operation identifier
Cache reserve
Manage Cache Reserve settings to store cached assets in Cloudflare’s persistent storage.
// Get Cache Reserve status
const status = await client.cache.cacheReserve.get({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
});
// Enable Cache Reserve
const response = await client.cache.cacheReserve.edit({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
value: 'on',
});
// Clear Cache Reserve
await client.cache.cacheReserve.clear({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
});
Smart tiered cache
Configure Smart Tiered Cache to optimize cache topology.
// Get Smart Tiered Cache settings
const settings = await client.cache.smartTieredCache.get({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
});
// Enable Smart Tiered Cache
const response = await client.cache.smartTieredCache.edit({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
value: 'on',
});
// Disable Smart Tiered Cache
await client.cache.smartTieredCache.delete({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
});
Regional tiered cache
Manage Regional Tiered Cache to improve cache hit ratios.
// Get regional tiered cache settings
const settings = await client.cache.regionalTieredCache.get({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
});
// Configure regional tiered cache
const response = await client.cache.regionalTieredCache.edit({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
value: 'on',
});
Cache variants
Manage cache variants to serve different versions of cached content based on image format or content encoding.
// Get cache variants configuration
const variants = await client.cache.variants.get({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
});
// Configure cache variants
const response = await client.cache.variants.edit({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
value: {
avif: ['image/avif', 'image/webp'],
webp: ['image/webp'],
},
});
// Delete cache variants configuration
await client.cache.variants.delete({
zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
});