I have a block that creates a private_tempstore value (which I understand works exactly like PHP sessions).

How do I unset that value.

This is the code I am using for the block.

class SsoSessionBlock extends BlockBase {
     * {@inheritdoc}
    public function build(){
        $value = 'dav';
        $session = \Drupal::service('user.private_tempstore')->get('achva_sso');
        $session->set('name', $value);

        return [
            '#type' => 'markup',
            '#markup' => $this->t('The output of super SsoSessionBlock block.'),

I saw this method, but I am not sure how I am supposed to use it.

I tried the following code, but I get an error.

namespace Drupal\achva_sso\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\user;

class SsoController extends ControllerBase{
    public function ssoLogout(){

        $session = \Drupal::service('user.private_tempstore')->get('achva_sso');

            $status = $session .' is logged in';
            $status = 'is logged out';
            '#type' => 'markup',
            '#markup' => '<p>' . $status . '</p>' ,


How do I unset a value I previously set?

2 Answers 2


I have a block that creates a private_tempstore value (which I understand works exactly like PHP sessions).

No, both are for different use cases.

Private tempstore is used mainly for larger quantities of data. To store a single value it would be too much overhead. Instead use the session object of the current request:

$request = \Drupal::request(); 
// in a class inject RequestStack and $request = $this->requestStack->getCurrentRequest();
// or get the request as controller parameter, see link below
$session = $request->getSession();

// set a value
$session->set('my_value', $my_value);

// unset a value

This is the official Drupal 8 way to store session data and since you've tagged the question with sessions this probably what you are looking for, see Access session data through the Request object

  • Hey,few questions, 1. Where do I get the set function now that I have removed the ` \Drupal::service('user.private_tempstore')->get('achva_sso');` 2. I have added the folowing namespace use Symfony\Component\HttpFoundation\RequestStack; in the block file, than inside the build function I wrote this code: ` $request = \Drupal::request(); $session = $request->getSession('name');` and than I return the $session var. I keep getting an error
    – DavSev
    Commented Nov 15, 2018 at 8:16
  • this is the error Recoverable fatal error: Object of class Symfony\Component\HttpFoundation\Session\Session could not be converted to string in Drupal\Component\Utility\Xss::filter() (line 67 of core\lib\Drupal\Component\Utility\Xss.php).
    – DavSev
    Commented Nov 15, 2018 at 8:17
  • The code in the answer works as it is. Dependency injection is a different issue and you can include this as a TODO comment for later when you want to test the class.
    – 4uk4
    Commented Nov 15, 2018 at 8:22

delete() is the method to delete a value in the store.

$session = \Drupal::service('user.private_tempstore')->get('achva_sso');

'name' is the value passed as first argument in any call to set().

You don't use PrivateTempStore::delete() since it's not a static method and it would throw errors or warnings, if called as static method.

As side note, user.private_tempstore is a deprecated service. Instead, the tempstore.private service should be used. (See User module's user.private_tempstore and user.shared_tempstore services moved to core for more details.) In the latest Drupal 8.5.x version, the service is marked as deprecated directly in the user.services.yml file.

class: Drupal\user\PrivateTempStoreFactory
  - '@keyvalue.expirable'
  - '@lock'
  - '@current_user'
  - '@request_stack'
  - '%tempstore.expire%'
deprecated: 'The "%service_id%" service is deprecated. You should use the ''tempstore.private'' service instead. See https://www.drupal.org/node/2935639.'
  - { name: backend_overridable }

It should also be better to use dependency injection: A class extending BlockBase, or ControllerBase, can implement the create() method to load new dependencies.

  • Hey, Thank you for your answer. Tried this but the tempstore is not deleted. I created a new page with the $session->get('name') . I go to the page with the delete, than go back to the page with the get and I still see the name value...
    – DavSev
    Commented Nov 15, 2018 at 8:44
  • 1
    Keep in mind that when a user logs out, or logs in, the tempstore is changed. If, for example, you set a value when the user is logged-in, and delete the same value after the user logged out, that value is still associated to the user account.
    – apaderno
    Commented Nov 15, 2018 at 8:48
  • o.k I undestand. thank you... now I see I asked the wrong question... I need to store a session for an anonymus user... thank you!
    – DavSev
    Commented Nov 15, 2018 at 8:50
  • @kiamlaluno thanks your answer was very useful to me. Commented May 18, 2020 at 10:24

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.