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.

1 Answer 1


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'];
    Database.Delete(sharesToDelete, false);
  • It gives me an error saying "DELETE_FAILED, cannot delete owner or rule share rows". Commented Sep 22, 2015 at 14:23
  • Nevermind, I got it. I was missing the rowcause condition. Commented Sep 22, 2015 at 16:02
  • @Saroj Bera can we revoke access by using apex class?? and i am trying to use dynamic values
    – user48859
    Commented Oct 9, 2017 at 15:23
  • @SFDCDeveloper What do you mean by dynamic values?
    – Saroj Bera
    Commented Oct 9, 2017 at 18:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .