Skip to content

Instantly share code, notes, and snippets.

@isalmanhaider
Created July 26, 2024 13:02
Show Gist options
  • Save isalmanhaider/41649e279442cd355229fce2db6f7c89 to your computer and use it in GitHub Desktop.
Save isalmanhaider/41649e279442cd355229fce2db6f7c89 to your computer and use it in GitHub Desktop.
delete node from content and group_content
<?php
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
use Drupal\node\Entity\Node;
// Bootstrap Drupal.
$autoloader = require_once __DIR__ . '/vendor/autoload.php';
$kernel = DrupalKernel::createFromRequest(Request::createFromGlobals(), $autoloader, 'prod');
$kernel->boot();
// Node ID to delete.
$nid = 292921; // Replace with your actual node ID
// Load the node.
$node = Node::load($nid);
if ($node) {
// Delete group content associated with the node.
$group_contents = \Drupal::entityTypeManager()
->getStorage('group_content')
->loadByProperties(['entity_id' => $nid]);
foreach ($group_contents as $group_content) {
$group_content->delete();
}
// Delete the node.
$node->delete();
echo "Node with ID $nid and its group content have been deleted.\n";
} else {
echo "Node with ID $nid does not exist.\n";
}
@isalmanhaider
Copy link
Author

drush scr delete_node.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment