How to programmatically delete a display from a view? I am trying to programmatically delete an existing view’s display (at the same time as deleting a node) but I am not able to find the good implementation.

HOOK_node_delete(Drupal\Core\Entity\EntityInterface $node) {
//[…]
$view = Views::getView("VIEW_MACHINE_NAME");
$display_name = "block_1";
$views_display = $view-&gt;storage-&gt;getDisplay($display_name);
//[…]


I can access to the view’s display using the view entity (storage) but as the display has protected access I can’t delete it.

Any help?
DisplayPluginCollection has a remove() method:

$view-&gt;displayHandlers-&gt;remove($display_id);


But this doesn't work and I couldn't find any code which is trying to actually remove the display from configuration.

So you would have to do this yourself in the view entity (storage):

$view = \Drupal\views\Views::getView('myview');
$storage = $view-&gt;storage;
$displays = $storage-&gt;get('display');
unset($displays[$display_id]);
$storage-&gt;set('display', $displays);
$storage-&gt;save();


The problem is this will not run the remove code of the display. For a block display this doesn't matter, but if you delete a page display you might consider running the remove method mentioned above before deleting the display from storage. This will delete the menu links from the page.