# Purge unused realms

**URL:** <https://forum.keycloak.org/t/purge-unused-realms/3053>\
**Category:** Getting advice\
**Tags:** admin-console, admin-rest, database\
**Created:** [June 1, 2020, 6:56am UTC](https://forum.keycloak.org/t/purge-unused-realms/3053 "2020-06-01T06:56:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![upendra](https://avatars.discourse-cdn.com/v4/letter/u/e68b1a/32.png) [@upendra](https://forum.keycloak.org/u/upendra)\
**Post date:** [June 1, 2020, 6:56am UTC](https://forum.keycloak.org/t/purge-unused-realms/3053/1 "2020-06-01T06:56:37Z")

</div>

I have keycloak instance with over 2000 realms. There are many realms that are not in use (old, realms created for testing etc). Is there a mechanism to find these realms and delete them manually or automatically based on the last used time stamp or any other attribute. Any help on this would be much appreciated. Thank you in advance.

---

<div class="post-metadata">

**Author:** ![zonaut](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/zonaut/32/666_2.png) [@zonaut](https://forum.keycloak.org/u/zonaut)\
**Post date:** [June 1, 2020, 9:13am UTC](https://forum.keycloak.org/t/purge-unused-realms/3053/2 "2020-06-01T09:13:22Z")

</div>

You can call the admin/realms endpoint and get a list of all realms.  
Go over them and check against one of the properties you need.

The Keycloak REST API is not fully documented so it’s better to dive into the source code.  
The keycloak-admin-client is the best resource in my opinion.  
A couple of examples on how to use it [Keycloak Admin Client in Spring Boot](http://forum.keycloak.org/t/keycloak-admin-client-in-spring-boot/2547/2)

You could just do the following based on the examples in the link I posted

```
RealmsResource realmsResource = keycloak.realms();
for (RealmRepresentation realmRepresentation : realmsResource.findAll()) {
    // Do your thing
}
```

---

<div class="post-metadata">

**Author:** ![upendra](https://avatars.discourse-cdn.com/v4/letter/u/e68b1a/32.png) [@upendra](https://forum.keycloak.org/u/upendra)\
**Post date:** [June 5, 2020, 6:20am UTC](https://forum.keycloak.org/t/purge-unused-realms/3053/3 "2020-06-05T06:20:29Z")

</div>

Thanks @zonaut for your reply. I have used REST API for keycloak before but have not used Admin client. So have very less idea on getting started with it. I reffered the link you shared ([Keycloak Admin Client in Spring Boot](http://forum.keycloak.org/t/keycloak-admin-client-in-spring-boot/2547/2)). I am quite sure of doing the things at the keycloak side as you mentioned in your examples but not the code part on from where to run.

---

<div class="post-metadata">

**Author:** ![zonaut](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/zonaut/32/666_2.png) [@zonaut](https://forum.keycloak.org/u/zonaut)\
**Post date:** [June 5, 2020, 7:19am UTC](https://forum.keycloak.org/t/purge-unused-realms/3053/4 "2020-06-05T07:19:38Z")

</div>

If you don’t know Java you can try the nodejs alternative found in [https://github.com/keycloak/keycloak-nodejs-admin-client](https://github.com/keycloak/keycloak-nodejs-admin-client), I’ve never used it so I don’t know how complete it is.

You can also look into the source code under [https://github.com/keycloak/keycloak/tree/master/integration/admin-client/src/main/java/org/keycloak/admin/client/resource](https://github.com/keycloak/keycloak/tree/master/integration/admin-client/src/main/java/org/keycloak/admin/client/resource) and see each available path that you could use.

realmsResource.findAll() = /admin/realms

---

<div class="post-metadata">

**Author:** ![upendra](https://avatars.discourse-cdn.com/v4/letter/u/e68b1a/32.png) [@upendra](https://forum.keycloak.org/u/upendra)\
**Post date:** [June 5, 2020, 7:34am UTC](https://forum.keycloak.org/t/purge-unused-realms/3053/5 "2020-06-05T07:34:44Z")

</div>

Great @zonaut. Thanks much for your help 🙂

---

<div class="post-metadata">

**Author:** ![thibaud.benoist](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@thibaud.benoist](https://forum.keycloak.org/u/thibaud.benoist)\
**Post date:** [July 20, 2021, 5:54am UTC](https://forum.keycloak.org/t/purge-unused-realms/3053/6 "2021-07-20T05:54:42Z")

</div>

Hi upendra, can you tell me how you managed to create 2000 realms in keycloak ? I use the version 11 and I am limited to nearly 400 realms.

Your answer could save my life 🙂

Best regards,  
Thibaud
