How To Clear The Cache in ServiceNow

Clearing the instance cache is a crucial component of being a ServiceNow Admin/Developer. If you don’t understand how and when to clear the cache, you might be wasting a lot of time chasing a customization …


Buy The "ServiceNow Developer's Manual" Now

We've packed over a decade of ServiceNow Development advice, into a single e-book.

Buy It Now

Clearing the instance cache is a crucial component of being a ServiceNow Admin/Developer.

If you don’t understand how and when to clear the cache, you might be wasting a lot of time chasing a customization that you made, and you don’t understand why it’s not showing up.

I have seen a lot of ServiceNow People just randomly clear the cache when things aren’t working properly. This isn’t always the best idea.

While doing a cache flush doesn’t have any real negative consequences, make sure you know when and why you’re doing it.

This will keep your instance running performantly and efficiently for all your customers.

Clearing the cache in DEV/TEST/STAGE is no big deal, but if you clear the instance cache in production, and have a lot of users – these users will experience small performance degradation and a user experience, because the instance doesn’t have any data in cache and needs to spend time/bandwidth to retrieve the data from the server now – it can no longer rely on the instance cache.

What Is A Cache Flush?

In general, a cache is a temporary storage area where frequently accessed data can be stored for rapid access.

The browser cache helps improve web page loading speed by storing certain files locally on your computer. When you visit a web page, your browser checks the cache to see if it has a recent copy of the files required to display the page.

When most people hear of a cache, they think of a browser cache and are aware that the browser that you use, stores data and files for easier retrieval, so pages load faster. If you visit a page often, the images on the page will be stored in local memory, and if the server isn’t cache flushed, then your browser will use what’s in cached memory to serve pages faster.

This is the benefit of the cache and the reason why browsers usually cache webpages.

For a ServiceNow instance, it works a little differently.

The ServiceNow instance stores data in a cache, for faster performance. Like form layouts and other frequently used functionality.

When a cache is cleared/flushed, ServiceNow can no longer rely on it’s cache to serve up data to the end user. So the instance needs to make more calls to the server to get the data.

You should clear an instance’s cache when you have made a customization correctly, but the instance just isn’t rendering it properly.

Consider the following example of ACL’s and a ServiceNow instance cache.

ServiceNow caches ACL’s for users.

When modifying something like an ACL, you may need to clear the cache of the instance, so it uses the most up to date ACL data.

I’ve experienced issues when writing ACL scripts. I had sworn the ACL “should have worked” but it wasn’t. The customization was done correctly, but I wasn’t seeing the result in ServiceNow as I’d expected, when impersonating the user.

When I did a cache flush in the instance, the ACL’s were refreshed and the customization worked as expected.

Cache flush when you know you did the right thing, but ServiceNow isn’t reflecting it.

How To Clear the ServiceNow Cache

I’m aware of 3 different ways to clear the instance cache in ServiceNow.

All 3 of these options do the exact same thing (clear the instance cache), they’re just different ways to do it.

Some may be slightly faster than others, and all are very simple and straightforward.

Go To /cache.do.

Simple navigate to this URL in your instance:

https://instance-name.service-now.com/cache.do

When you navigate here, you’ll see the cache flush data for the instance, like below:

servicenow cache flush /cache.do

Run gs.cacheFlush() In A Background Script or Server Side Script

This option is going to be helpful if you ever need to perform the operation in a script.

Consider the example where you wanted to clear the instance cache after a certain operation in a script, or if you wanted to clear the instance cache on a nightly basis.

You’d simply take gs.cacheFlush(); and place it into your script.

This can be done in any server side script: background script, business rule, script include, processing script, UI Action (server only), etc.

This CANNOT be done in a client side script. So it will not work in a: client script, UI script, etc.

If you wanted to clear the instance cache on a recurring or nightly basis, just create a scheduled job in ServiceNow, and have it run nightly.

In the script section, simply put the one liner:

gs.cacheFlush();

If you need/want a log for this, do the following:

gs.cacheFlush();
gs.log("Successfully cleared the cache at: " + gs.nowDateTime());

When you run gs.cacheFlush(); in a background script, or any server side script, you’ll get the following output:

Starting cache flush
Flushing cache
Loading properties from DB
Cache flush complete
Loaded the Data Lookup cache in 14 ms

Go to the Cache Module

cache flush module in servicenow

This is probably the easiest way, and requires no scripting.

Simply type cache in the application navigator, and you’ll see System Diagnostics > Cache.

When you click on Cache, it will clear the instance cache automatically and instantly.

That’s it, you now know of a few more ways to clear the cache than you likely did a few minutes ago.

Let us know of other times when you have had to clear the cache of the instance.

We’d love to hear your thoughts on cache flushing.



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Kailas
Kailas
1 year ago

Dear Team

Is there any way to delete the cookies in the browser and can do script in the server side.

Any help will be appropriated.

Thanks,kailas

2
0
Would love your thoughts, please comment.x
()
x