Purging Element Caches with the Java SDK

The other day I had a requirement where I needed to give a single user access to clear Element Caches.  He didn’t have a Desktop license, but the permission to access the Caches isn’t tied to Desktop.  With a quick program written in Java using the MicroStrategy Java SDK, I was able to deploy a quick app to the user to execute a cache clear on their own.

The permissions required to clear the caches aren’t tied to any license types.  They are under the Administration section of the Security Role, and they should be: Administer Caches, Configure Caches and Monitor Caches.  Add these as a new Security Role or directly to a new Group, and then add your target user to that group.

Java Code
I think this code is derived from a MicroStrategy Sample, though I can’t find it now to give it proper credit. In any case, I’ve adjusted the code for my purposes to purge the Element Caches, though if you’re interested in purging more caches, they should be simple modifications.

For background information on setting up your Environment to run this code, check out the post Getting Started with the Java Web SDK.

package Cache;

import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectsException;
import com.microstrategy.web.objects.WebObjectsFactory;
import com.microstrategy.web.objects.admin.WebObjectsAdminException;
import com.microstrategy.web.objects.admin.monitors.CacheResults;
import com.microstrategy.web.objects.admin.monitors.CacheSource;
import com.microstrategy.web.objects.admin.monitors.Caches;
import com.microstrategy.web.objects.admin.monitors.EnumWebMonitorType;
import com.microstrategy.web.objects.admin.monitors.JobSource;
import com.microstrategy.webapi.EnumDSSXMLLevelFlags;
import com.microstrategy.webapi.EnumDSSXMLPurgeFlag;

public class ClearCache {

private static WebObjectsFactory factory = null;
private static WebIServerSession serverSession = null;

public static void main(String[] args) throws WebObjectsException, IllegalArgumentException {
// create a WebObjectsFactory instance
//WebObjectsFactory factory = WebObjectsFactory.getInstance();

// Get the cache source object
CacheSource source = (CacheSource) factory.getMonitorSource(EnumWebMonitorType.WebMonitorTypeCache);

// Set up the flag to obtain browsing information

// Since we only need to get the project DSSID in order to purge its object cache,
// so we set the incremental fetch setting to the bare minimum

try {
     // Sends the request to Intelligence Server to retrieve cache information
     CacheResults results = source.getCaches();
     // Cache are grouped in project level, so we loop through the caches collection
     // to obtain their project DSSID and then purge the object cache immediately
     for (int i = 0; i < results.size(); i++) {
          // Obtain the project DSSID of each caches collection
          Caches projectLevelCaches = results.get(i);
          String projectDSSID = projectLevelCaches.getProjectDSSID();
          if (projectDSSID.equals("0114E056402ECFF8D895DAB005972880") || projectDSSID.equals("3BA2B0C848B17C4C96C18C8FD390237E")) //Dev and Prod
          // Purge the element cache of each project
          source.purgeCache(projectDSSID, EnumDSSXMLPurgeFlag.DssXmlPurgeElementCache);
          System.out.println("Purged Cache for " + projectDSSID);
} catch (WebObjectsAdminException woae) {

public static void getSession() {
System.out.println("Connecting to MicroStrategy");
factory = WebObjectsFactory.getInstance();
serverSession = factory.getIServerSession();
serverSession.setProjectName("PROJECT"); //just a project to connect to, not the cache we're going to clear (see above for projects to clear)

try {
} catch (WebObjectsException ex) {
System.out.println("Error: " + ex.getMessage());


Obviously if the user has Desktop access, the permissions outlined at the start could be used to access the regular Cache monitor.  And if you need to automate cache clearing, Command Manager is a better option that writing all of this code.  However, there was a niche for me for this code, and maybe at the very least it’ll serve as an example if you’re interested in related Java SDK development.

You may also like...