Clear a Field in Drupal Using Drush

Clear a Field in Drupal Using Drush

Do you want to delete a value from a Drupal field? This article shows an easy way using Drush.
Author
mcruzv
Tags
Time to read
2 min

How to clear a field value with a PHP file and Drush

This guide shows a simple way to remove (clear) a field value in Drupal. We will write a small PHP file and run it with Drush.

Before you start

  • Make a database backup.
  • Test on a dev site first.
  • Know the field machine name. Example: field_image or field_teaser.

What we will do

  1. Create a PHP file.
  2. Run it with Drush: drush scr.
  3. Check the node. The field is empty now.

Option A: Clear one node

Use this when you want to clear the field on a single node (one page).

  1. Create a file, for example: scripts/clear_field_one.php in your Drupal root.
  2. Paste this code:
<?php
      use Drupal\node\Entity\Node;
      
      /**
      * Run with: drush scr scripts/clear_field_one.php
      *
      * Change these two values:
      *  - $nid: the node ID.
      *  - $field: the field machine name.
      */
      
      $nid   = 123;             // <-- change this
      $field = 'field_teaser';  // <-- change this
      
      $node = Node::load($nid);
      
      if (!$node) {
          echo "Node not found: $nid\n";
          return;
      }
      
      if (!$node->hasField($field)) {
          echo "Field not found on node: $field\n";
          return;
      }
      
      // Clear value (works for single or multi-value fields).
      $node->set($field, NULL);
      // You can also use: $node->set($field, []);
      $node->save();
      
      echo "Field '$field' cleared on node $nid\n";
  
  1. Run it:
drush scr scripts/clear_field_one.php

Go to the node and check. The field is empty now.


Option B: Clear many nodes (by content type)

Use this when you want to clear the field on many nodes. Example: all “blog” nodes.

  1. Create a file: scripts/clear_field_many.php
  2. Paste this code:
<?php
    use Drupal\Core\Entity\EntityTypeManagerInterface;

    /**
    * Run with: drush scr scripts/clear_field_many.php
    * Change these values: 
    * $content_type: your bundle (e.g., 'blog'). 
    * $field: the field machine name.
    */
    
    $content_type = 'blog'; // <-- change this
    $field = 'field_teaser'; // <-- change this
    $storage = \Drupal::entityTypeManager()->getStorage('node'); // Get all node IDs for the content type (you can add conditions if needed).
    $nids = $storage->getQuery()->condition('type', $content_type)->accessCheck(false); // run as admin in script ->execute();
    if (empty($nids)){
        echo "No nodes found for type: $content_type\n";
        return;
    }
    $batch = 0;
    foreach (array_chunk($nids, 50) as $chunk){
        $nodes = $storage->loadMultiple($chunk);
        foreach ($nodes as $node){
            if ($node->hasField($field)){
                $node->set($field, NULL);
                $node->save();
                echo "Cleared '$field' on node {$node->id()}\n";
            }
        }
        $batch++;
    }
    echo "Done. Content type: $content_type. Field: $field.\n";
  1. Run it:
drush scr scripts/clear_field_many.php

Check a few nodes to confirm.


Notes

  • For image/file fields you may also want to delete the file. That is a different task.
  • If you want to set a default value (not empty), use: $node->set('field_name', 'new text');
  • For multi-value fields you can clear one item like: $node->get('field_tags')->removeItem(0);
  • Add a log message if you want: \Drupal::logger('maint')->notice('Cleared field on @nid', ['@nid' => $node->id()]);

Troubleshooting

  • “Node not found” → Check the node ID.
  • “Field not found” → Check the field machine name and the bundle.
  • No change after run → Clear caches: drush cr. Then check again.