Delete node references for some content types only
Your code can be made more concise using Drupal APIs. It's rare that DB queries are required in Drupal 8, as the system generally has APIs that handle it for you. Here's how I'd do what you're trying to do. I think the key you were looking for is the line with in_array().

// Load all 'some_content_type' nodes.
$properties = [
  'type' =&gt; 'some_content_type',
];
$nodes = \Drupal::entityTypeManager()-&gt;loadStorage('node')-&gt;loadByProperties($properties);

foreach ($nodes as $node) {
  // Loop through the entities referenced by the field.
  foreach ($node-&gt;get('my_reference_field')-&gt;referencedEntities() as $entity) {
    // Only act if the entity is not one of the given types.
    if (!in_array($entity-&gt;bundle(), ['type_1', 'type_2'])) {
      // Delete the entity.
      $entity-&gt;delete();
    }
  }
}