As the title states, we have several installed packages that requires licenses for users to access the package. If someone leaves the company and we deactivate their Salesforce user, do their licenses get removed as well or do we have to manually remove them from each package?


[D]o their licenses get removed as well[?]


[D]o we have to manually remove them from each package?


Allow me to explain. The license assignments remain when a user is deactivated. However, you can write a trigger or Process Builder (via a flow) to remove those assigned licenses when a user is deactivated. This is how we keep our license count for paid licenses to just active users.

This is the relevant code in our user trigger framework that handles this:

static void removeInactiveLicenses(User[] records) {
    User[] inactives = new User[0];
    for(User record: records) {
        if(!record.IsActive) {
    if(!inactives.isEmpty()) {
        delete [SELECT Id FROM PermissionSetAssignment WHERE AssigneeId IN :inactives AND PermissionSet.IsOwnedByProfile = FALSE];
        delete [SELECT Id FROM PermissionSetLicenseAssign WHERE AssigneeId IN :inactives];
        delete [SELECT Id FROM UserPackageLicense WHERE UserId IN :inactives];

This removes all permission sets, assigned licenses, and assigned packages.

| improve this answer | |
  • Thanks! If I wanted to also remove the user from any Public Groups they're in, would I just add this line? delete [SELECT Id FROM Group WHERE UserId IN :inactives]; Do I have to incorporate the UserOrGroupId field on the GroupMember object? – silkwood Feb 21 '17 at 19:42
  • Also, how the heck do I format my comment?! – silkwood Feb 21 '17 at 19:43
  • 1
    Oh I think I figured it out, is it this? delete [SELECT Id FROM GroupMember WHERE UserOrGroupId IN :inactives]; – silkwood Feb 21 '17 at 19:47
  • @JTang Yes, that last query should work. It usually doesn't matter, because groups can be of unlimited size, but isn't particularly harmful. – sfdcfox Feb 21 '17 at 20:15
  • I am trying to use the code as is and I am getting this error. Error: Compile Error: sObject type 'UserPackageLicense' is not supported. at line 65. Can you please help. – Shweta Feb 27 '17 at 22:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.