Suppose you used to have a module that defined a layout called layout_twocol_maxwidth. Suppose also that you uninstalled that module, and the new name for this layout is layout_twocol_any. If you try to access a node that uses the old layout, the page will not render (error code 500). How would you replace all occurrences of the old layout with the new layout? Below is the solution. I've verified this solution to have worked on a drupal 10 site. I'm using a multi-site drupal install but if you don't, then you can remove "-l wasyaco.com" from the command.
drush -l wasyaco.com php:eval '
$tables = [
"node__layout_builder__layout",
"node_revision__layout_builder__layout",
"block_content__layout_builder__layout",
"block_content_revision__layout_builder__layout",
"taxonomy_term__layout_builder__layout",
"taxonomy_term_revision__layout_builder__layout",
];
$database = \Drupal::database();
$reflection = new \ReflectionProperty(
\Drupal\layout_builder\Section::class,
"layoutId"
);
$reflection->setAccessible(true);
$total = 0;
foreach ($tables as $table) {
$rows = $database->select($table, "t")
->fields("t", ["entity_id", "revision_id", "langcode", "delta", "layout_builder__layout_section"])
->execute();
$updated = 0;
foreach ($rows as $row) {
$section = unserialize($row->layout_builder__layout_section);
if (!$section instanceof \Drupal\layout_builder\Section) {
continue;
}
if ($section->getLayoutId() !== "layout_twocol_maxwidth") {
continue;
}
$reflection->setValue($section, "layout_twocol_any");
$database->update($table)
->fields([
"layout_builder__layout_section" => serialize($section),
])
->condition("entity_id", $row->entity_id)
->condition("revision_id", $row->revision_id)
->condition("langcode", $row->langcode)
->condition("delta", $row->delta)
->execute();
$updated++;
}
print "$table: updated $updated sections\n";
$total += $updated;
}
print "Total updated: $total\n";
'After running the above command (feel free to modify according to your needs, the two interesting keywords are layout_twocol_maxwidth and layout_twocol_any), you can check that this layout is no longer referenced in the database:
drush -l wasyaco.com sqlq "
SELECT 'node' AS entity, COUNT(*) AS remaining
FROM node__layout_builder__layout
WHERE layout_builder__layout_section LIKE '%layout_twocol_maxwidth%'
UNION ALL
SELECT 'block_content', COUNT(*)
FROM block_content__layout_builder__layout
WHERE layout_builder__layout_section LIKE '%layout_twocol_maxwidth%'
UNION ALL
SELECT 'taxonomy_term', COUNT(*)
FROM taxonomy_term__layout_builder__layout
WHERE layout_builder__layout_section LIKE '%layout_twocol_maxwidth%'
"
And don't forget to clear the cache:
drush -l wasyaco.com cr.