I'm looking for a way to delete all Sessions except the newest Session for each User managed by KeyCloak

Currently, I am using KeyCloak to manage sessions for application users.
However, there is a problem that KeyCloak’s memory capacity is getting high now.

Therefore, my team are considering a method to delete unnecessary sessions to allow more memory capacity.

To achieve this, we are considering the following method.
・Delete all Session except the newest Session for each user
Please tell me the feasible method .


I know how to delete all Sessions for each user by using this API. But I don’t know how to keep the latest Session.

POST / {realm} / users / {id} / logout

unable to delete all session for each user. throw error.
“error”: “HTTP 401 Unauthorized”

This might be handy for your needs: