2016-01-04 50 views
6

我需要刪除Drupal 8(有超過7k節點)相同類型的所有節點。Drupal 8:刪除相同類型的所有節點

這對Drupal 7來說不是問題(DB查詢+ node_delete或node_delete_multiple可以解決我的問題)。但是,D8略有不同:)

請教,我該怎麼辦。提前致謝!

回答

7

那麼,答案就在於表面上:

$types = array('my_content_type_name'); 

$nids_query = db_select('node', 'n') 
->fields('n', array('nid')) 
->condition('n.type', $types, 'IN') 
->range(0, 500) 
->execute(); 

$nids = $nids_query->fetchCol(); 

entity_delete_multiple('node', $nids); 

我建議你使用「範圍」和一些「批處理」(或者只是重新運行該代碼多次),因爲它是一個非常胖的操作(每個操作500個節點對於256MB是可以的)。

要執行這個代碼,您可以編寫自定義模塊或使用devel的模塊:https://www.drupal.org/project/devel

安裝完成後,轉至yoursite_address/devel的/ PHP和執行PHP代碼出現。

+1

謝謝,真的很好。你也可以使用drush 8:'drush src yourscript.php' –

8

應該使用實體的查詢,而不是直接對數據庫的作用:

$result = \Drupal::entityQuery('node') 
     ->condition('type', 'my_content_type_name') 
     ->execute(); 
    entity_delete_multiple('node', $result); 

在對方的回答設置範圍等應該不會太困難。

請參閱EntityFieldQuery has been rewritten瞭解更多信息。

+0

它不會像查詢+額外的包裝器一樣嗎?難道它不會比數據庫查詢的性能更強,難道你不知道嗎? – megastruktur

+0

我不知道,但我猜這種方法實際上是更高性能的,因爲它允許API中的緩存層,如果直接進行數據庫調用將不會激活。 – colan

5

Drupal的8具有的功能來獲得由內容類型的節點,所以我會用

$nodes = \Drupal::entityTypeManager() 
    ->getStorage('node') 
    ->loadByProperties(array('type' => 'your_content_type')); 

foreach ($nodes as $node) { 
    $node->delete(); 
} 
2

的entity_delete_multiple被棄用的Drupal 8.0.x的,將Drupal的9.0.0之前被刪除。使用實體存儲的delete()方法刪除多個實體:

// query all entities you want for example taxonomy term from tags vocabulary 
$query = \Drupal::entityQuery('taxonomy_term'); 
$query->condition('vid', 'tags'); 
$tids = $query->execute(); 

$storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type); 
$entities = $storage_handler->loadMultiple($tids); 
$storage_handler->delete($entities); 
0

刪除某些實體類型的所有實體,我用這個片段改編自最後的評論:

$entity_types = ['taxonomy_term','node','menu_link_content',]; 
foreach ($entity_types as $entity_type) { 
    $query = \Drupal::entityQuery($entity_type); 
    $ids = $query->execute(); 

    $storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type); 
    $entities = $storage_handler->loadMultiple($ids); 
    $storage_handler->delete($entities); 
} 
2

可以使用Devel module
去管理 - >配置 - >開發 - >生成內容
(管理/配置/開發/生成/內容)
2-選擇您想要刪除其節點的內容類型。
3-檢查「刪除這些內容類型的所有內容。」(重要)
4-放「0」在「多少個節點你想生成」(重要)
查看附加圖片的說明。

attached image

0

非常簡單的方法就是安裝Bulk Delete模塊。它適用於D7 & D8。

安裝模塊後,您將在點擊內容菜單時看到批量刪除節點選項卡選項。

它節省了我的一天:)

爲了您的方便,我附上了截圖。

enter image description here

相關問題