function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Apoorva SharmaApoorva Sharma 

Delete failed. First exception : DELETE_FAILED, cannot delete owner or rule share rows

Hi All,

I am trying to Programmatically delete Sharing Rules with Apex. I am  getting the following exception cannot delete owner or rule share rows. Below is my code:

public static void RestrictShare(List<Account> slist){
      List<ID> shareIdsToDelete = new List<ID>();
      
      for (Approval_Stage__c stage:slist) {
      if (stage.Share__c == false) {

            shareIdsToDelete.add(stage.id);
      }
    }
    if (!shareIdsToDelete.isEmpty()){
      delete [select id from AccountShare where AccountShare.Id IN :shareIdsToDelete];
      }
  }

I am calling it in a after update trigger
Frédéric TrébuchetFrédéric Trébuchet
Hi,

It seems that you have to delete the object record while deleting share records for that object.
Have a look at http://salesforce.stackexchange.com/questions/9076/revoking-apex-managed-sharing-permission.

Hope this helps,
Fred
Frédéric TrébuchetFrédéric Trébuchet
Hi,

If these answers helped you solve your problem, please, mark the question as Solved and kindly select the best one ;)

Thanks,
Fred
Ariel GorfinkelAriel Gorfinkel
To summarize the link that Frédéric Trébuchet referred to, one needs to add 
AND RowCause = 'Manual'

to the query of share objects.
Thanks for your answer.