How to drop Keycloak theme cache without clearing browser data

Andrey Kotov
1 min readDec 16, 2021

It’s a very common case for production: you are uploading new CSS or JS files with significant changes of Keycloak theme, but the old version is already cached in visitors’ browsers, and you have nothing to do except to ask to clear cache.

Keycloak recommends to disable caching for development process, but to use cache in production:

<theme>
<staticMaxAge>-1</staticMaxAge>
<cacheThemes>false</cacheThemes>
<cacheTemplates>false</cacheTemplates>
...
</theme>

You can decrease staticMaxAge in advance for planned update, but sometimes you need to roll out new changes very fast.

URL of theme resource looks like this:

<link href=”/auth/resources/s5yk7/account/keycloak.v2/public/layout.css” rel=”stylesheet”>

Highlighted part is called “resource tag”, and if you know how to make Keycloak to change tag after your update, that makes URLs unique therefore not cached.

Looking at MigrationModelAdapter code, we realise that new resource tag is being generated during migration, but why not to generate new tag by ourselves?

Connect to your database instance,

[keycloak]> select * from MIGRATION_MODEL;+ — — — - + — — — — -+ — — — — — -+
| ID | VERSION | UPDATE_TIME |
+ — — — -+ — — — — -+ — — — — — — -+
| 6b7re | 10.0.2 | 1592806968 |
| kyvg0 | 12.0.4 | 1617619612 |
| s5yk7 | 15.0.2 | 1637069930 |
+ — — — -+ — — — — -+ — — — — — — -+

Just update ID with new value, generate it by yourself:

update MIGRATION_MODEL set ID='s5yk8' where ID='s5yk7';

and restart Keycloak instance.

--

--