I have a custom field associated with all posts and pages. It's a dropdown of all the navigation menus. 
here is how I'm populating the custom field drop-down: (the field name is custom\_menu...)

```
function acf_load_menu_field_choices( $field ) {

    // reset choices
    $field['choices'] = array();

    $menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );

    $blank_list = json_encode(array( "name" => "Select Menu", "slug" => "")); 
    $blank_list = json_decode($blank_list);
    array_unshift($menus, $blank_list);
    foreach ( $menus as $val ) {

        $value = $val->slug;
        $label = $val->name;

        $field['choices'][ $value ] = $label;
    }

    // return the field
    return $field;

}
add_filter('acf/load_field/name=custom_menu', 'acf_load_menu_field_choices');

```

Here is a common menu location that I'm using on each page:

```
function register_custom_menu() {  //function to register new menu
     register_nav_menu('custom-menu',__( 'Custom Menu' ));
}
add_action( 'init', 'register_custom_menu' );

```

And then I'm assigning a menu dynamically to the location `custom-menu` based on the custom field menu on each page.

and here is the function the fires on each page when it's loaded:

```
add_action("wp_ajax_load_custom_menu", "load_custom_menu");
add_action("wp_ajax_nopriv_load_custom_menu", "load_custom_menu");
function load_custom_menu(){
    $post_id = $_POST['page_id'];
    $page_custom_menu = get_field('custom_menu', $post_id);
    if(empty($page_custom_menu) || $page_custom_menu == "primary") return;
    $locations = get_theme_mod( 'nav_menu_locations' );
    if(!empty($locations)) {
        foreach($locations as $locationId => $menuValue) { 
            if($locationId == "custom-menu") $menu = get_term_by('slug', $page_custom_menu, 'nav_menu');  
            if(isset($menu)) { $locations[$locationId] = $menu->term_id; }
        } 
    } 
    set_theme_mod('nav_menu_locations', $locations);

    wp_nav_menu( array( 
        'theme_location' => 'custom-menu', 
        'menu_id' => 'primary-menu', 
        'menu_class' => 'main-nav underline-decoration l-to-r-line level-arrows-on outside-item-remove-margin',
        'container' => false
        ) 
    );

    wp_die();
}

```

This ajax function fires on `ready` event and recieves a `POST` value called `page_id`. This function checks the custom field value of the given `page_id` for `custom-menu` and assign that menu to the `menu location` called `custom-menu`.

Here's my JavaScript in case you need to have a look on it:

```
jQuery(document).ready( function($){
    let customMenu;
    let page_id = script_vars.postID;
    $.post(dtLocal.ajaxurl, { action: "load_custom_menu", page_id: page_id }, resp => customMenu = resp);
    $(window).scroll(() => {
        if(customMenu !== "0"){
            $("#phantom .main-nav").remove();
            $("#phantom .menu-box").html(customMenu);
        }
    })
})

```

This is how my code is working, And everything works fine up to this point.

**Now here is the problem**
Whenever I delete a Navigation menu from **Appearence>Menus>Delete Menu** My custom Menu starts behaving unexpectedly because the custom field value is still pointing to the deleted Menu.
What I want to do here is, **I want to Delete the Custom Field whenever a menu is deleted**. *First I want to get the slug of a deleted menu and then find the custom field value with that slug and then finally, delete or reset that custom field.*