Hi @Oussamatahiaida,
I believe your requirement can be easily achieved using features available in the UI itself. Here is the link to recently announced feature: Remove All Access with a workaround to keep a few entitlements: New Capability: Remove All Access on Termination - #3 by NataliaYunusov
Basically, you do not include lifecycle state: “inactive“ in your role criteria and add the entitlements you want the inactive user to keep in an “Access Profile“. Add the Access Profile to inactive lifecycle state so the user retain them.
Good luck
