How can I clear the Menu cache after saving some configuration? I'm in the process of creating my first D7 module, partly based on the tutorial found here. My module creates (among other things) a public page to show some content, and it also has a configuration page (in the Drupal admin) that allows the user to choose the path at which this page must be displayed (e.g. "mypage" or "content/mymodule" etc).

My problem is that saving this value through the configuration form is not enough: you also have to clear the Menu cache, otherwise the old path remains active, and the new one gives you a "page not found" error.

I need that to occur automatically every time this value is changed (or every time the form is submitted). I tried using menu_rebuild() in my validation function, but it's obviously not the right place, because it is triggered before the new data is saved, and so the menu is rebuilt with the obsolete data.

How should I proceed if I want the Menu cache to be automatically cleared after the data has been validated and saved?

Here's my implementation of the pages, via hook_menu:

&lt;?php

/**
 * Implements hook_menu().
 */
function mymodule_menu() {
  $items = array();

  // config page
  $items['admin/config/services/mymodule'] = array(
    'title' =&gt; 'mymodule',
    'description' =&gt; 'Configuration for the mymodule module',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('mymodule_form'),
    'access arguments' =&gt; array('access administration pages'),
    'type' =&gt; MENU_NORMAL_ITEM,
  );

  // public page
  $m_title = variable_get('mymodule_title', 'Mymodule');
  $m_title = $m_title ? $m_title : 'Mymodule';

  $m_path = variable_get('mymodule_path', 'mymodule');
  $m_path = $m_path ? $m_path : 'mymodule';

  $items[$m_path] = array(
      'title' =&gt; $m_title,
      'page callback' =&gt; '_mymodule_page',
      'access arguments' =&gt; array('access mymodule content'),
      'type' =&gt; MENU_NORMAL_ITEM,
  );

  return $items;
}


/**
 * Page callback: mymodule settings
 *
 * @see mymodule_menu()
 */
function mymodule_form($form, &amp;$form_state) {
  $form['mymodule_title'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Title displayed on the mymodule public page'),
    '#default_value' =&gt; variable_get('mymodule_title', 'Mymodule'),
    '#required' =&gt; TRUE,
  );
  $form['mymodule_path'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Path for the mymodule public page'),
    '#default_value' =&gt; variable_get('mymodule_path', 'mymodule'),
    '#required' =&gt; TRUE,
  );
  $form['#submit'][] = 'mymodule_form_submit';

  return system_settings_form($form);
}


/**
 * Add a submit handler/function to the form.
 */
function mymodule_form_submit($form, &amp;$form_state) {
  drupal_set_message(t('TEST: The form has been submitted.'));
  menu_rebuild();
}


/**
 * Implements validation from the Form API.
 *
 * @param $form
 *   A structured array containing the elements and properties of the form.
 * @param $form_state
 *   An array that stores information about the form's current state
 *   during processing.
 */
function mymodule_form_validate($form, &amp;$form_state) {
  // page path validation
  $m_path = $form_state['values']['mymodule_path'];
  $m_path_db = variable_get('mymodule_path', '');
  if ($m_path_db != $m_path) {

    if (drupal_lookup_path('source', $m_path) || menu_get_item($m_path)) {
      form_set_error('mymodule_path', t('This path is already used.'));
    } else {

      // attempting to validate path value
      // see: http://drupal.stackexchange.com/questions/167923
      $m_path_parts = explode('/', $m_path);
      $has_invalid_chars = false;
      foreach ($m_path_parts as $key =&gt; $part) {
        if (urlencode($part) != $part) {
          $has_invalid_chars = true;
        }
      }
      if ($has_invalid_chars) {
        form_set_error('mymodule_path', t('This path contains invalid characters.'));
      }
    }
  }
}

?&gt;


Edit: more code provided.
Your form code would be helpful to see here to determine if you are using a system_settings_form() function (which seems likely since you are creating a module configuration form) or just a simple form. Regardless, the answer for both is to add a submit_handler to your form.

Basic Form with Submit Handler: https://www.drupal.org/node/717740

If you are not using system_settings_form, than you can add your post-submit actions to a function named after your form definition function, or 

/**
 * Add a submit handler/function to the form.
 */
function my_form_submit($form, &amp;$form_state) {
  &lt;YOUR menu_rebuild STUFF HERE&gt;
}


and Form API will automatically find it. If you are using system_settings_form() for your admin form, than you can add your submit handler by adding this before calling system_settings_form()

$form['#submit'][] = 'my_form_submit';