Replace Menu via Code

Hi guys,
i would like to replace the menu with an other existing menu if conditions are fulfilled.

i tried to alter this with these hooks, but doesnt seem to work.

function mymodule_block_view_system_menu_block_alter (array &$build, DrupalCoreBlockBlockPluginInterface $block) {
 $block->setConfigurationValue("id", "system_menu_block:main2");
 $build = $block->build();

function mymodule_block_build_system_menu_block_alter (array &$build, DrupalCoreBlockBlockPluginInterface $block) {

tried to change some values here, but the only thing happed were that the menu disappeared.
hope you can help me.


Drupal version: