I removed the Administer site configuration permission from a role, but I need users with that role to flush the cache from /admin/config/development/performance.

I know, there is hook_menu_alter() in Drupal 7, but what about Drupal 8?


1 Answer 1


You can alter the permission used to access a route, as described in Altering existing routes and adding new routes based on dynamic ones. In the specific case, the code used in alterRoutes(RouteCollection $collection) would be the following one.

Since the administer site performance permission is a custom one, it needs to be added as new permission by the module that implements the route subscriber class, as described in How to create a custom permission on Drupal 8 - 9.

For a module whose machine name is mymodule, the code would be similar to the following one.


administer site performance:
  title: 'Administer site performance'
  description: 'Access the site performance settings page.'
  restrict access: true


    class: Drupal\mymodule\Routing\RouteSubscriber
      - { name: event_subscriber }


namespace Drupal\mymodule\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

 * Listens to the dynamic route events.
class RouteSubscriber extends RouteSubscriberBase {

   * {@inheritdoc}
  protected function alterRoutes(RouteCollection $collection) {
    if ($route = $collection->get('system.performance_settings')) {
      $route->setRequirement('_permission', 'administer site performance');


If the users with that role should not access the settings in that page (Caching and Bandwidth optimization), but only be able to clear the cache, I would implement a custom route that shows a confirmation form and clear the cache (which means calling drupal_flush_all_caches()) when the user confirms the operation.

  • Thanks! Works like a charm. Commented Aug 17, 2021 at 16:50
  • That would be an awesome answer for drupal.stackexchange.com/questions/234270/…
    – leymannx
    Commented Aug 17, 2021 at 18:24
  • @leymannx I should expand the answer to show more code than it does, including the code to use in the second scenario.
    – apaderno
    Commented Aug 17, 2021 at 18:31
  • The current suggestion is exactly what I was looking for. Commented Aug 17, 2021 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.