function field_purge_batch

Same name in other branches
  1. 7.x modules/field/field.crud.inc \field_purge_batch()
  2. 8.9.x core/modules/field/field.purge.inc \field_purge_batch()
  3. 10 core/modules/field/field.purge.inc \field_purge_batch()
  4. 11.x core/modules/field/field.purge.inc \field_purge_batch()

Purges a batch of deleted Field API data, field storages, or fields.

This function will purge deleted field data in batches. The batch size is defined as an argument to the function, and once each batch is finished, it continues with the next batch until all have completed. If a deleted field with no remaining data records is found, the field itself will be purged. If a deleted field storage with no remaining fields is found, the field storage itself will be purged.

Parameters

int $batch_size: The maximum number of field data records to purge before returning.

string $field_storage_unique_id: (optional) Limit the purge to a specific field storage. Defaults to NULL.

Related topics

15 calls to field_purge_batch()
BulkDeleteTest::testPurgeField in core/modules/field/tests/src/Kernel/BulkDeleteTest.php
Tests purging fields.
BulkDeleteTest::testPurgeFieldStorage in core/modules/field/tests/src/Kernel/BulkDeleteTest.php
Tests purging field storages.
BulkDeleteTest::testPurgeWithDeletedAndActiveField in core/modules/field/tests/src/Kernel/BulkDeleteTest.php
Tests that recreating a field with the name as a deleted field works.
CommentFieldsTest::testCommentInstallAfterContentModule in core/modules/comment/tests/src/Functional/CommentFieldsTest.php
Tests that comment module works when installed after a content module.
CommentUninstallTest::testCommentUninstallWithoutField in core/modules/comment/tests/src/Kernel/CommentUninstallTest.php
Tests if uninstallation succeeds if the field has been deleted beforehand.

... See full list

File

core/modules/field/field.purge.inc, line 74

Code

function field_purge_batch($batch_size, $field_storage_unique_id = NULL) {
    
    /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */
    $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');
    $fields = $deleted_fields_repository->getFieldDefinitions($field_storage_unique_id);
    foreach ($fields as $field) {
        $entity_type = $field->getTargetEntityTypeId();
        $count_purged = \Drupal::entityTypeManager()->getStorage($entity_type)
            ->purgeFieldData($field, $batch_size);
        if ($count_purged < $batch_size || $count_purged == 0) {
            // No field data remains for the field, so we can remove it.
            field_purge_field($field);
        }
        $batch_size -= $count_purged;
        // Only delete up to the maximum number of records.
        if ($batch_size == 0) {
            break;
        }
    }
    // Retrieve all deleted field storages. Any that have no fields can be purged.
    foreach ($deleted_fields_repository->getFieldStorageDefinitions() as $field_storage) {
        if ($field_storage_unique_id && $field_storage->getUniqueStorageIdentifier() != $field_storage_unique_id) {
            // If a specific UUID is provided, only purge the corresponding field.
            continue;
        }
        $fields = $deleted_fields_repository->getFieldDefinitions($field_storage->getUniqueStorageIdentifier());
        if (empty($fields)) {
            field_purge_field_storage($field_storage);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.