[SalesForce] Revoke record sharing access using Apex

I am sharing records of a custom object using User Managed Sharing through an Apex class

   CustomObj__Share teamShare  = new CustomObj__Share();

  // Set the ID of record being shared.
  teamShare.ParentId = customObj.Id;

  // Set the ID of user or group being granted access.
  teamShare.UserOrGroupId = user.Id;

  // Set the access level.
  teamShare.AccessLevel = 'Read';

  // Set rowCause to 'manual' for manual sharing.
  // This line can be omitted as 'manual' is the default value for sharing objects.
  teamShare.RowCause = Schema.CustomObj__Share.RowCause.Manual;

  // Insert the sharing record and capture the save result. 
  // The false parameter allows for partial processing if multiple records passed 
  // into the operation.
  Database.SaveResult sr = Database.insert(teamShare,false);

Everything works fine till here. When I log in as the user, I can see the custom object record but read only.

Now I need to do the same thing, but to revoke this given read (or edit) access to that user. In the documentation it says there is an access level named None, but it is only used for AccountShare.

How do I revoke access to the custom object using Apex? This apex code is called by a vf page where a user can grant/revoke access to the objects records using checkbox (or dropdown). So I am looking to solve revoke access using this vf page or a trigger.

Appreciate the help.

Best Answer

I think you can do something like this as described by Mark Pond in this link.

List<MyCustomObject__Share> sharesToDelete = [SELECT Id 
                                                FROM MyCustomObject__Share 
                                                WHERE ParentId IN :trigger.newMap.keyset() 
                                                AND RowCause = 'Manual'];
if(!sharesToDelete.isEmpty()){
    Database.Delete(sharesToDelete, false);
}