Subscribe Now: Feed Icon

Wednesday, November 23, 2011

Clearing ArcGIS Server REST API Cache

Whenever you change a layer in the DB or publish a new server you might notice that the REST API will not automatically update. In order for it to update you might need to clear the REST API cache.

There are several ways to do so:

The default way:

ArcGis Server comes with a REST API admin page, which can be found here:


Logging in to this page gives you the Clear Cache Options:

The page has a bunch of options for clearing the catch semi-automatically:

What you need to do is simply click on the "Clear Cache Now" several times (at some version of the Server one time was not enough…).


1. You need to have the admin user/password in order to clear the cache
2. The process needs to be done manually.
3. If you have multiple server you will have to do so for each server


The token way:

ESRI gives another way which is to use a generated token and then you can simply call a link whenever you want to clear the cache. More on that here (ESRI refers to this method as “the easy way”).


1. Different token for each server
2. The process needs to be manually set at each server


With Console Application (with code):

The problem with all of these methods is that you can’t really use them to install servers, at my company we have more servers then flies and having to clear the cache after each installation is tedious work that is often forgotten (and when it is forgotten I find myself wasting hours trying to debug the “problem”). So I decided to write a console application that does the clearing of the cache for me. This idea actually came from this forum post, the only problem was that the code didn’t really work for me…

So I tweaked it a bit with WebRequest and the end result is this:

  1. public static string ClearCache(string serverName, string userName, string password)
  2. {
  3.     var result = HttpUtils.PostRequest("http://" + serverName + "/ArcGIS/rest/admin/login",
  4.                             new List<KeyValuePair<string, string>>
  5.                                 {
  6.                                     new KeyValuePair<string, string>("username", userName),
  7.                                     new KeyValuePair<string, string>("password", password),
  8.                                     new KeyValuePair<string, string>("redirect", String.Format("http%3a%2f%2f{0}%2fArcGIS%2frest%2fadmin%2fcache%2fclear", serverName)),
  9.                                 }, String.Format("http://srvp7d-gtm/ArcGIS/rest/admin/login?redirect=http%3a%2f%2f{0}%2fArcGIS%2frest%2fadmin%2fcache%2fclear", serverName));
  11.     //The response is in html, only return the response if its not a success
  12.     return result.Contains("Cache Cleared.") ? "Cache Cleared." : result;
  13. }


And HttpUtils.PostRequest looks like:

  1. public static string PostRequest(string url, List<KeyValuePair<string, string>> paramters, string referer)
  2. {
  3.     var req = WebRequest.Create(url);
  4.     req.ContentType = "application/x-www-form-urlencoded";
  5.     req.Method = "POST";
  7.     var parameters = GetParameters(paramters);
  9.     //We need to count how many bytes we're sending.
  10.     var bytes = Encoding.ASCII.GetBytes(parameters);
  11.     req.ContentLength = bytes.Length;
  13.     ((HttpWebRequest)req).CookieContainer = new CookieContainer();
  14.     if (!String.IsNullOrEmpty(referer))
  15.         ((HttpWebRequest)req).Referer = referer;
  17.     var os = req.GetRequestStream();
  18.     os.Write(bytes, 0, bytes.Length); //Push it out there
  19.     os.Close();
  20.     var resp = req.GetResponse();
  21.     if (resp == null)
  22.         return null;
  23.     var sr = new StreamReader(resp.GetResponseStream());
  24.     return sr.ReadToEnd().Trim();
  25. }

I think that this way is the best since all I need is the server name, the admin user and it’s password. I can do it remotely and I can do it automatically after each deploy. The only thing that is not so good is the using of the clear password (and lets be honest here the next step is putting the call to the console application in a batch file for the development environment and giving that file to the rest of the team so that a hacker…).

Source code can be found at my Codeplex project, here (the code has a few more useful commands like publishing a MSD file, changing the connection of MSD file etc.).



Clearing the ArcGIS Services Directory cache “the easy way” – ArcGIS Server Blog

Clear cache through code by demand – ArcGis Server Forum

Hanselman: HTTP POSTs and HTTP GETs with WebClient and C# and Faking a PostBack


Keywords: ESRI,ArcGIS Server,REST API

IceRocket Tags: ,,