How delete a user session with the element api? I need a function to logout a craft user and delete the session of that user. This must take place in the element API plugin as an endpoint. Something like this:

function userLogout(){
    if (craft()-&gt;userSession-&gt;isLoggedIn()){
        // if user loggedin
        // how to logout?
    }
}

'api/logout' =&gt; array(
    'elementType' =&gt; 'User',
    'criteria' =&gt; ['id' =&gt; craft()-&gt;userSession-&gt;user-&gt;id, 'limit' =&gt; 1 ],
    'paginate' =&gt; false,
    'transformer' =&gt; function(\Craft\UserModel $user) {
        userLogout();
    }
),
You can end a user's session with the craft()-&gt;userSession-&gt;logout(), method, which the UserSessionService class inherits from the CWebUser class.

So, your userLogout function would look like this:

function userLogout(){
    if (craft()-&gt;userSession-&gt;isLoggedIn()){
        craft()-&gt;userSession-&gt;logout(false);
    }
}


Additional information

Passing false to the logout method tells Yii not to "destroy" the session, and mimics the call in Craft's UsersController::logout method.

According to the Yii documentation, destroy "frees all session variables and destroys all data registered to a session." I'm unclear why Craft chooses not to destroy the session.

The UsersController::logout method also includes a few cleanup tasks, but those seem to be duplicates of the tasks performed in the UserSessionService::afterLogout method.

The afterLogout method is called from the craft()-&gt;userSession-&gt;logout() method, so you shouldn't have to worry about this manual cleanup.