title: How to delete all objects through a core data relationship?
body: <p>Say I have a custom NSManagedObject <code>Department</code> and this has a property representing a to-many relationship to employees i.e. <code>NSSet *employees;</code>.</p>
  
  <p>For a given Department, I want to remove <em>all</em> objects in employees.  What is the recommended/best way to do this, please?</p>
  
  <p>So, hypothetically, my code would look like this:</p>
  
  <p><strong>Department.h</strong></p>
  
  <pre><code>@interface Department: NSManagedObject {
  }
  @property (retain) NSString *departmentName;
  @property (retain) NSSet *employees;
  @end
  </code></pre>
  
  <p><strong>Department.m</strong></p>
  
  <pre><code>@implementation Department
  @dynamic departmentName;
  @dynamic employees;
  </code></pre>
  
  <p><strong>Employee.h</strong></p>
  
  <pre><code>@interface Employee: NSManagedObject {
  }
  @property (retain) NSString *firstName;
  @property (retain) NSString *lastName;
  @property (retain) Department *worksIn;
  @end
  </code></pre>
  
  <p><strong>doCoreDataStuff</strong></p>
  
  <pre><code>- (void)doCoreDataStuff:sender {
      //add a department, give it a couple employees, then try to remove those employees
      NSEntityDescription *deptEntity = [NSEntityDescription entityForName:@"Department"
                                                   inManagedObjectContext:self.managedObjectContext];
      Department *dept = [Department alloc] initWithEntity:deptEntity
                            insertIntoManagedObjectContext:self.managedObjectContext];
      NSError *error;
  
      dept.departmentName = @"Accounting";
      //save more often than normal to see more easily what causes error
      if (![self.managedObjectContext save:&amp;error]) NSLog(@"\nError: %@", [error localizedDescription]);
  
      NSEntityDescription *empEntity = [NSEntityDescription entityForName:@"Employee"
                                                   inManagedObjectContext:self.managedObjectContext];
      emp.firstName = @"Steve";
      emp.lastName = @"Smith";
      emp.worksIn = dept;
  
      if (![self.managedObjectContext save:&amp;error]) NSLog(@"\nError: %@", [error localizedDescription]);
  
      emp = [[Employee alloc] initWithEntity:empEntity
              insertIntoManagedObjectContext:self.managedObjectContext];
      emp.firstName = @"Natasha";
      emp.lastName = @"Johnson";
      emp.worksIn = dept;
  
      if (![self.managedObjectContext save:&amp;error]) NSLog(@"\nError: %@", [error localizedDescription]);
  
      //all good so far! now will try to delete all employees for this department
      dept.employees = [NSSet set];
      if (![self.managedObjectContext save:&amp;error]) NSLog(@"\nError: %@", [error localizedDescription]); //"Multiple validation errors occurred."
  
      //this also produces the same error
      [[dept mutableSetValueForKey:@"employees"] removeAllObjects];
      if (![self.managedObjectContext save:&amp;error]) NSLog(@"\nError: %@", [error localizedDescription]); //"Multiple validation errors occurred."
  </code></pre>
  
  <p>The relationship <code>employees</code> is not optional so I'm guessing that removing employees from the department means that I am trying to "orphan" the employees i.e. keep employees in the persisted model without an associated department.</p>
  
  <p>So, I think my original question should be reworded to: what is best/recommended way to remove all "child" objects of a "parent" when the children have a non-optional relationship with the parent?</p>
  
  <p>I suspect that the answer is going to be "loop through and delete the employee objects one at a time".</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>According to an answer and a link to Apple's documentation, I should be able to set the delete rule to "Cascade" and then code like <code>department.employees = [NSSet set];</code> will work.  However, this does not work in my very simple project where I have set the delete rule accordingly.</p>
  
  <p>Thanks</p>