Click here to Skip to main content
Click here to Skip to main content

How to Purge Cache in Akamai Proxy Server using C#

, 24 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use C# to programmatically purge cache of the Akamai proxy server

Purge Request to Akamai

Purge request to Akamai server is a process to send instruction to the proxy server to free-up some item in the cache so that the new request will get a new copy of data. This is an HTTP command used by Akamai proxy server. Purge cache in Akamai proxy server is very easy, all you need to know is what programming language you use and add the appropriate Web Service.

Akamai Web Service

Language URL
Microsoft Visual Basic 6, SOAP 1.0
Microsoft Visual Basic 6, SOAP 3.0
Microsoft Visual Basic .NET

Steps to Purge Cache in Akamai Proxy Server in C#

1. Open Microsoft Visual Studio ( I am using 2008)

I am going to use C#, so I need to run Visual Studio to create a project and write code to purge cache items:

2. Add Akamai Web Service

Akamai is exposing one of their web services that has functionality to trigger purge cache request in Akamai proxy server. All we need to do is to add this service as reference in our .NET application.

Right click on the C# project and then click on the Add Service Reference item, then the Add Service Reference popup window will appear.

In the Address input box, please input and then click on the Go button to load the web service. At the bottom, you'll see Namespace input box you can modify the namespace a sper your convenience, let's say AkamaiPurgeCache. You will be using this namespace upon accessing the methods and classes inside this service.

3. Write code to initiate purge request to Akamai proxy server

Please modify the needed information, especially the credentials and the item you want to purge.

public void PurgeCache() 
        string username = "username"; 	//use the username to access 
        string password = "password"; 	//use the password to access the control.
        string email = ","; //for multiple emails use comma as separator 
        string[] fileToPurge = new string[]
	{""}; //this is the item in 
					//akamai proxy that you want to purge
        string action = "action=remove"; 	//other value is action=invalidate 
        string domain = "domain=production";//other value is domain=staging 
        //type: type=cpcode or type=arl >> arl: akamai resource locator. 
        //url: uniform resource locator. 
        //To use the type cpcode option, your administrator must enable 
        //purge-by-cpcode access for your username 
        //through Akamai EdgeControl. 
        string purgeType = "type=arl"; //other value is type=cpcode 
        string[] options = new string[]{action, domain, email, purgeType}; 
        AkamaiPurgeCache.PurgeApi purgeAPI = new AkamaiPurgeCache.PurgeApiClient(); 
        AkamaiPurgeCache.PurgeResult purgeResult = purgeAPI.purgeRequest
		(username, password, string.Empty, options, fileToPurge); 
        if (purgeResult.resultCode >= 300) 
            System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
            sb.AppendFormat("Error Code: {0}",purgeResult.resultCode); 
            sb.AppendFormat("Error Message: {0}", purgeResult.resultMsg); 
            sb.AppendFormat("Session ID: {0}", purgeResult.sessionID); 
            sb.AppendFormat("URI Index: {0}", purgeResult.uriIndex); 
            throw new Exception(sb.ToString()); 
    catch(Exception e) { //exception code here.. } 


  • 23rd April, 2011: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jephunneh Malazarte
Software Developer (Senior)
Philippines Philippines
Watch Anime...

Comments and Discussions

QuestionGeting this error | System.ServiceModel.EndpointNotFoundException PinmemberSweety .12-Mar-14 23:41 
QuestionIn option of "email", you should add a title. Pinmemberjealter15-Jan-14 21:09 
Questionthrow new Exception(sb.ToString()) actually throws exception Pinmembermykey20053-Jul-13 4:47 
AnswerRe: throw new Exception(sb.ToString()) actually throws exception Pinmembermykey20053-Jul-13 6:26 
GeneralRe: throw new Exception(sb.ToString()) actually throws exception PinmemberJephunneh Malazarte24-Jul-13 0:37 
QuestionHow to clear cache at folder level PinmemberUser@Rupesh6-May-13 9:03 
QuestionFor website Akamai cache key is different Pinmemberanushripatil6-Dec-11 0:45 
AnswerRe: For website Akamai cache key is different PinmemberJephunneh Malazarte6-Dec-11 3:27 
GeneralRe: For website Akamai cache key is different Pinmemberanushripatil6-Dec-11 18:10 
Questionthank you so much for this code! PinmemberMember 820973715-Oct-11 23:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 24 Apr 2011
Article Copyright 2011 by Jephunneh Malazarte
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid