Cloudflare provides security, CDN and more for your websites. If you’re using the Cloudflare caching to speed up your sites (it really is fast) you may want to purge their cache from your application (instead of waiting X days). Cloudflare provides an API that seems to offer everything you’d possible need. I wanted to do this from c#, but did’t find any great libraries or code that was using their newest API (v4).
Below is some quick simple code that I’ve found to work great so far for me. It’s pretty basic, and doesn’t require many external libraries (just JSON.net). Let me know your thoughts and if it helps you.
//define somethings we'll need for the api string apiEndpoint = "https://api.cloudflare.com/client/v4"; //user info here string userEmail = "youremail@something.com"; //this is your Global api key found in "my account" string userAPIkey = "xxxxxxxxxxxxxxxxxxxxxxxxx"; //domain your working with: string domain = "http://www.yourdomain.com"; //let's get our zone ID (we'll need this for other requests HttpWebRequest request = WebRequest.CreateHttp(apiEndpoint + "/zones?name=" + domain + "/&status=active&page=1&per_page=20&order=status&direction=desc&match=all"); request.Method = "Get"; request.ContentType = "application/json"; request.Headers.Add("X-Auth-Email", userEmail); request.Headers.Add("X-Auth-Key", userAPIkey); string srZoneResult = ""; using (WebResponse response = request.GetResponse()) using (var streamReader = new StreamReader(response.GetResponseStream())) srZoneResult = (streamReader.ReadToEnd()); dynamic zoneResult = JsonConvert.DeserializeObject(srZoneResult); if (zoneResult.result != null) { //get our zoneID string zoneId = zoneResult.result[0].id; //some pages to purge the cache on: byte[] data = Encoding.ASCII.GetBytes(@"{""files"":[""http://www.yourdomain.com/about/""]}"); request = WebRequest.CreateHttp(apiEndpoint + "/zones/" + zoneId + "/purge_cache"); request.Method = "DELETE"; request.ContentType = "application/json"; request.ContentLength = data.Length; request.Headers.Add("X-Auth-Email", userEmail); request.Headers.Add("X-Auth-Key", userAPIkey); using (Stream outStream = request.GetRequestStream()) { outStream.Write(data, 0, data.Length); outStream.Flush(); } string srPurgeResult = ""; using (WebResponse response = request.GetResponse()) using (var streamReader = new StreamReader(response.GetResponseStream())) srPurgeResult = (streamReader.ReadToEnd()); dynamic purgeResult = JsonConvert.DeserializeObject(srPurgeResult); //was it a success (hopefully it = true) textBox1.Text = purgeResult.success; }
Will this clear all the assets cache within the page too? So in your example you have “http://www.yourdomain.com/about/” and you are purging that cache. Will that clear the cache of all images, stylesheets on that page?
LikeLike
Lee, in my experience and usage of this, it only clears the file(s) you specify. It doesn’t use wildcards or look at page assets. You’d need to pass in an array with all the files you’d like to clear if you want more than a page cleared. Or pass purge_everything = true to clear the entire site. Hope this helps!
LikeLiked by 1 person
Ok thanks. I have an idea to solve this using HTMLAgility pack 🙂 Thanks for the code example.
LikeLiked by 1 person
Html Agility Pack is the best. I use it all of the time for a ton of things.
LikeLike
Hi Lee, can i have sample code to use HTMLAgility pack clear the cache.
LikeLike
How to implement purge for multiple domain dynamically? in asp.net mvc
LikeLike