Purge all files for a path via the API

Hello

I am trying to see if we can purge all files from a path via the API. When I try something like this:

curl -X POST "https://api.cloudflare.com/client/v4/zones/IDENTIFIER/purge_cache" \
 -H "Content-Type:application/json" \
 -H "X-Auth-Key:KEY" \
 -H "X-Auth-Email:EMAIL" \
	 --data '{"files":["https://example.com/trill", {"url":"https://example.com/trill"}]}'

It doesn’t seem to do the trick. Any advice would be helpful

Sorry, Purge by URL can’t do anything resembling a wildcard. It has to be a specific URL. To purge all files in a path, you would have to issue a command for each individual file.

Thats too bad. Is there a way to get all the files in a path via the API?

Sorry, no.

  1. Cloudflare doesn’t know all the files you would have in that directory.
  2. Cloudflare cache is distributed, so it’s not going to query all the data centers to find out what all is cached there at the moment.

Your only choices are to Purge Everything, or create a fancy local script that lists all the files in that directory, then loops through the API command for each file.

Hi @labboy0276,

As I’m not any good at cURL commands and API requests, I use Postman to make pretty much all my Cloudflare API requests. Looking at your question and @sdayman answer, I got curious about how the app handled this, as I have been using for a while a command to purge all pages from specific hosts within some of my zones.

All the Cloudflare API requests I make were imported from the Cloudflare collection for Postman, then adjusted with my API keys, email etc.

Both the Cloudflare collection for Postman and the API documentation have this command with POST as the method, but somehow I now noticed that it’s changed in the Postman app to a DELETE.

And the list of files is included as the “Body” in a comma-separated list. Notice that the “files” object does not contain the URL part. Yet it works, at least from within the Postman app:

{
"files":[
	"https://www.example.com/",
	"https://www.example.com/some-post/",
	"https://www.example.com/another-post/",
	"https://www.example.com/some-file.js",
	"https://www.example.com/another-file.css"]
}

Postman itself translates this back to curl as the following (I have removed the Postman token line):

curl -X DELETE \
  https://api.cloudflare.com/client/v4/zones/{{zone_id}}/purge_cache \
  -H 'Content-Type: application/json' \
  -H 'X-Auth-Email: [email protected]' \
  -H 'X-Auth-Key: {{auth_key}}' \
  -H 'cache-control: no-cache' \
  -d '{
"files":[
	"https://www.example.com/",
	"https://www.example.com/some-post/",
	"https://www.example.com/another-post/",
	"https://www.example.com/some-file.js",
	"https://www.example.com/another-file.css"]
}'

(I tried this, but got all kinds of error msgs, all related to “bad request”, so I guess my inexperience with handling curl won’t let me test this)

1 Like

Thanks @floripare I tried something similar. It does return a success but the page itself is still cached after I just tested it.

Interesting. I had tested this a few times before I posted, even as a way to test purging the cache for some files, as I was only doing HTML purges so far. It all worked. I now tested again in a test site, visited from a VPN and from my actual location, ran the command on Postman and both files I requested purged were in fact purged on both locations.

1 Like

Interesting, I will stare more on my end.