diff --git a/simplesamlphp_auth.module b/simplesamlphp_auth.module
index 85b1b1a..0fcf8d2 100644
--- a/simplesamlphp_auth.module
+++ b/simplesamlphp_auth.module
@@ -31,7 +31,7 @@
 
 use Drupal\Core\Url;
 use Drupal\Core\Form\FormStateInterface;
-use Drupal\Core\Link;
+use Drupal\Core\Session\AnonymousUserSession;
 
 /**
  * Implements hook_help().
@@ -53,13 +53,17 @@ function simplesamlphp_auth_user_logout($account) {
 
   $logout_url = \Drupal::config('simplesamlphp_auth.settings')->get('logout_goto_url');
   $simplesaml = \Drupal::service('simplesamlphp_auth.manager');
+  $session = \Drupal::service('session_manager');
+
 
   // Have to destroy the session here as some configurations of
   // SimpleSAMLphp_auth can create infinite loops. By removing IdP auth before
   // Drupal auth, checks for local authentication will trigger before the
   // session is destroyed naturally. We must therefore destroy the session
   // manually here.
-  session_destroy();
+  // Copied from user.module method user_logout().
+  $session->destroy();
+  $account->setAccount(new AnonymousUserSession());
 
   if ($logout_url) {
     $simplesaml->logout($logout_url);
