private function cleanupCollection(&$collection, $classMetadata)
    {
        $notNullableStringProperties = array();
        $nullableStringProperties = array();
        $otherProperties = array();
        foreach ($classMetadata->getFieldNames() as $fieldName) {
            $fieldMapping = $classMetadata->getFieldMapping($fieldName);
            // skip id field
            if (isset($fieldMapping['id']) && $fieldMapping['id'] === true) {
                continue;
            }
            // skip language field
            if (in_array($fieldMapping['fieldName'], $this->autoRemoveIgnoreFields) || $fieldMapping['fieldName'] === $this->localePropertyPath) {
                continue;
            }

            if ($fieldMapping['nullable']) {
                if ($fieldMapping['type'] === 'string') {
                    $nullableStringProperties[] = $fieldMapping['fieldName'];
                } else {
                    $otherProperties[] = $fieldMapping['fieldName'];
                }
            } else {
                if ($fieldMapping['type'] === 'string') {
                    $notNullableStringProperties[] = $fieldMapping['fieldName'];
                } else {
                    $otherProperties[] = $fieldMapping['fieldName'];
                }
            }
        }

        $toRemove = array();

        foreach ($collection as $entity) { // entity is translation, i.e. $sitemapNodeTranslation
            $allNullableStringsAreNull = true;
            foreach ($nullableStringProperties as $pp) { // $pp is property path, i.e. 'short_description'
                // this calls something like $sitemapNodeTranslation->getShortDescription()
                $value = $this->propertyAccessor->getValue($entity, $pp);
                if (trim($value) === '') {
                    $this->propertyAccessor->setValue($entity, $pp, null);
                } else {
                    $allNullableStringsAreNull = false;
                }
            }

            $allNotNullableStringsAreNull = true;
            foreach ($notNullableStringProperties as $pp) {
                $value = $this->propertyAccessor->getValue($entity, $pp);
                if ($value !== null) {
                    if (trim($value) !== '') {
                        $allNotNullableStringsAreNull = false;
                    }
                } else {
                    $this->propertyAccessor->setValue($entity, $pp, '');
                }
            }

            $otherAreNull = true;
            foreach ($otherProperties as $pp) {
                $value = $this->propertyAccessor->getValue($entity, $pp);
                if ($value !== null) {
                    $otherAreNull = false;
                }
            }

            if ($allNullableStringsAreNull && $allNotNullableStringsAreNull && $otherAreNull) {
                $toRemove[] = $entity;
                if (null !== $this->objectManager && $this->objectManager->contains($entity)) {
                    $this->objectManager->remove($entity); // Delete from db.
                }
            }
        }

        $this->removeFromCollection($collection, $toRemove);
    }