Clear a Field in Drupal Using Drush
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_imageorfield_teaser.
What we will do
- Create a PHP file.
- Run it with Drush:
drush scr. - 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).
- Create a file, for example:
scripts/clear_field_one.phpin your Drupal root. - 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";
- Run it:
drush scr scripts/clear_field_one.phpGo 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.
- Create a file:
scripts/clear_field_many.php - 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";- Run it:
drush scr scripts/clear_field_many.phpCheck 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.