As I know there isn't a core/session in Magento 2 [source], I've created a custom session using customer/session as given below:

public function __construct(
    \Magento\Customer\Model\Session $customerSession,
) {
    $this->customerSession = $customerSession;
public function execute() {
    $this->customerSession->setOtp(5); // To set session value
    $this->customerSession->getOtp();  // To get session value

$this->customerSession->destroy(); will destroy whole customer session, which I do not want. Is it possible to destroy this particular otp session value automatically after 30 minutes?


1 Answer 1


You can remove/unset custom session via 'uns' prefix.


Hope this will help you.

  • Can you please let me know how to set the time after which it will be automatically destroyed?
    – amitshree
    May 23, 2018 at 7:27
  • 1
    I think you need to create a custom script and set this script to cron. Cron will run that script every 30min. thisinterestsme.com/expire-php-sessions this reference link will help you to check the time and unset session.
    – Nits
    May 23, 2018 at 7:45
  • I think there should be a more simpler solution to automatically unset this value?
    – amitshree
    May 24, 2018 at 12:13
  • No, I don't think so. Else you can use cookies that will be upset at the particular time.
    – Nits
    May 24, 2018 at 12:21

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.