# 69.4. Logout still linked to account

When a user creates a new account, the session retains the same `session_id` as the unauthenticated user. This can lead to changes being linked to both the unauthenticated state and the new account, even if the user logs out. This issue arises because the session is not reset upon account creation or logout, allowing data to persist across states.

An elegant solution to address this issue is to generate a new `session_id` whenever the user logs out. This ensures that the unauthenticated session is treated as entirely separate, effectively unlinking it from any previously authenticated account. By doing so, the data associated with the unauthenticated session will no longer persist across states. However, during the creation of a new account, relevant data from the session will still be passed to the new account, preserving a seamless user experience.

To implement this solution, we updated the `perform_logout` view function.

{% code title="views.py" overflow="wrap" lineNumbers="true" %}
```python
@login_required
def perform_logout(request):
    # Log out the user
    logout(request)

    # Clear all session data
    request.session.flush()

    # Clear the current session cookie
    response = redirect('perform_login')
    response.delete_cookie('id_session')

    return response

```
{% endcode %}

<figure><img src="https://1987892552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBsBTgwgH8PgXSXzgW9sj%2Fuploads%2Fbe1ur0bwzOyUNhIfASL0%2Fimage.png?alt=media&#x26;token=d2807007-347b-46ff-9aaf-a77e251af396" alt=""><figcaption><p>Now, whenever the user logs out, the new unauthenticated session will have a fresh <code>session_id</code>, unlinking both accounts.</p></figcaption></figure>
