session_unset

session_unset(): bool

Empties all session variables in $_SESSION while keeping the session and its storage alive (unlike session_destroy, which removes the storage); equivalent to $_SESSION = [] and only works while a session is active.

C C23

C23
#include <stdio.h>

/* in-process KV store standing in for $_SESSION */
static char keys[8][16];
static int vals[8];
static int n = 0;

int main(void) {
    keys[0][0] = 'a'; vals[0] = 1; n++;
    keys[1][0] = 'b'; vals[1] = 2; n++;
    printf("count before: %d\n", n);
    n = 0; /* unset: drop the entries, the store itself stays usable */
    printf("cleared: %d\n", n);
    keys[n][0] = 'c'; vals[n] = 3; n++; /* refill proves usability */
    printf("usable after: %s\n", "yes");
    printf("refilled: %d\n", n);
    return 0;
}
Output
count before: 2
cleared: 0
usable after: yes
refilled: 1

C++ C++26

C++26
#include <iostream>
#include <string>
#include <unordered_map>

int main() {
    // clear() on the container is the language-level counterpart of session_unset
    std::unordered_map<std::string, int> session;
    session["a"] = 1;
    session["b"] = 2;
    std::cout << "count before: " << session.size() << "\n";
    session.clear();
    std::cout << "cleared: " << session.size() << "\n";
    session["probe"] = 0; // the container object survives clearing
    session.erase("probe");
    std::cout << "usable after: yes\n";
    session["c"] = 3;
    std::cout << "refilled: " << session.size() << "\n";
    return 0;
}
Output
count before: 2
cleared: 0
usable after: yes
refilled: 1

C# v11.0.100

v11.0.100
// Clear() on the dictionary mirrors session_unset: entries gone, container alive
var session = new System.Collections.Generic.Dictionary<string, int>();
session["a"] = 1;
session["b"] = 2;
Console.WriteLine("count before: " + session.Count);
session.Clear();
Console.WriteLine("cleared: " + session.Count);
session["probe"] = 0; session.Remove("probe"); // still writable after Clear
Console.WriteLine("usable after: yes");
session["c"] = 3;
Console.WriteLine("refilled: " + session.Count);
Output
count before: 2
cleared: 0
usable after: yes
refilled: 1

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// Re-assigning an empty map mirrors session_unset: entries gone, variable alive
	session := map[string]int{}
	session["a"] = 1
	session["b"] = 2
	fmt.Println("count before:", len(session))
	for k := range session {
		delete(session, k)
	}
	fmt.Println("cleared:", len(session))
	session["probe"] = 0 // still writable after clearing
	delete(session, "probe")
	fmt.Println("usable after: yes")
	session["c"] = 3
	fmt.Println("refilled:", len(session))
}
Output
count before: 2
cleared: 0
usable after: yes
refilled: 1

Java v27.0.1

v27.0.1
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        // clear() on the map mirrors session_unset: entries gone, container alive
        Map<String, Integer> session = new HashMap<>();
        session.put("a", 1);
        session.put("b", 2);
        System.out.println("count before: " + session.size());
        session.clear();
        System.out.println("cleared: " + session.size());
        session.put("probe", 0); // still writable after clear()
        session.remove("probe");
        System.out.println("usable after: yes");
        session.put("c", 3);
        System.out.println("refilled: " + session.size());
    }
}
Output
count before: 2
cleared: 0
usable after: yes
refilled: 1

JavaScript v26.13.1

v26.13.1
// Map.clear() is the language-level counterpart; the session-scoped real equivalent
// is express-session's req.session = null / store-level clear (framework layer)
const session = new Map();
session.set("a", 1);
session.set("b", 2);
console.log("count before:", session.size);
session.clear();
console.log("cleared:", session.size);
let usable = true;
try { session.set("probe", 0); session.delete("probe"); } catch { usable = false; }
console.log("usable after:", usable ? "yes" : "no");
session.set("c", 3);
console.log("refilled:", session.size);
Output
count before: 2
cleared: 0
usable after: yes
refilled: 1

PHP v8.6.0

v8.6.0
// session_unset: empties $_SESSION while keeping the session itself active (storage is emptied at the next write).
// Unlike session_destroy(), which tears down the storage. Equivalent to $_SESSION = [].
ini_set("session.save_path", sys_get_temp_dir());
session_start();
$_SESSION["a"] = 1;
$_SESSION["b"] = 2;
$c1 = count($_SESSION);
$ok = session_unset();
$c2 = count($_SESSION);
$active = session_status() === PHP_SESSION_ACTIVE;
$_SESSION["c"] = 3;
session_write_close();
session_start();
$c3 = count($_SESSION);
session_destroy();
echo "count before: ", $c1, "\n";
echo "unset returns: ", var_export($ok, true), "\n";
echo "cleared: ", $c2, "\n";
echo "session after unset: ", $active ? "active" : "closed", "\n";
echo "refilled: ", $c3;
Output
count before: 2
unset returns: true
cleared: 0
session after unset: active
refilled: 1

Python v3.15.2

v3.15.2
# dict.clear() is the language-level counterpart; the real session equivalent is
# Flask's session.clear() (framework layer, keeps the cookie session alive)
store = {}
store["a"] = 1
store["b"] = 2
print("count before:", len(store))
store.clear()
print("cleared:", len(store))
usable = True
try:
    store["probe"] = 0
    del store["probe"]
except Exception:
    usable = False
print("usable after:", "yes" if usable else "no")
store["c"] = 3
print("refilled:", len(store))
Output
count before: 2
cleared: 0
usable after: yes
refilled: 1

Rust v1.99.1

v1.99.1
use std::collections::HashMap;

fn main() {
    // HashMap::clear is the language-level counterpart of session_unset
    let mut session: HashMap<String, i32> = HashMap::new();
    session.insert("a".to_string(), 1);
    session.insert("b".to_string(), 2);
    println!("count before: {}", session.len());
    session.clear();
    println!("cleared: {}", session.len());
    session.insert("probe".to_string(), 0); // still writable after clear
    session.remove("probe");
    println!("usable after: yes");
    session.insert("c".to_string(), 3);
    println!("refilled: {}", session.len());
}
Output
count before: 2
cleared: 0
usable after: yes
refilled: 1

TypeScript v26.13.1

v26.13.1
// Map.clear() is the language-level counterpart; the session-scoped real equivalent
// is express-session's req.session = null / store-level clear (framework layer)
const session = new Map<string, number>();
session.set("a", 1);
session.set("b", 2);
console.log("count before:", session.size);
session.clear();
console.log("cleared:", session.size);
let usable: boolean = true;
try { session.set("probe", 0); session.delete("probe"); } catch { usable = false; }
console.log("usable after:", usable ? "yes" : "no");
session.set("c", 3);
console.log("refilled:", session.size);
Output
count before: 2
cleared: 0
usable after: yes
refilled: 1

Language differences

C: no session concept — the language-level demo is zeroing counters/resetting a struct; the logout triple (unset + destroy + regenerate_id) must be orchestrated by hand in HTTP sessions.

C++: map::clear() keeps the container object, isomorphic to session_unset keeping the session; framework logout flows are orchestrated in Crow/Drogon-style libraries.

C#: the framework equivalent is ASP.NET's HttpContext.Session.Clear() (keys only, session kept); ISession has no destroy — dropping a session means clearing the cookie plus storage-side expiry.

Go: at the language level only map rebuild/key-by-key delete; gorilla-sessions' session.Options.MaxAge < 0 plus session.Save() is the idiomatic "drop the session" combo.

Java: at the language level Map.clear(); Servlet logout is session.invalidate() (the real destroy) plus request.changeSessionId(), matching PHP's unset/destroy split one-to-one.

JavaScript: at the language level Map.clear(); express-session's req.session.destroy(cb) is the real destroy, req.session.regenerate() maps to session_regenerate_id; req.session = null only clears request-scoped data.

PHP: session_unset equals $_SESSION = [] — clears data, keeps the session; session_destroy removes the storage and kills the session; the logout idiom is the triple session_unset() + session_destroy() + session_regenerate_id(true). Never unset($_SESSION) as a whole (that breaks the superglobal), and session_unset does nothing when no session is active (use $_SESSION = [] there).

Python: at the language level dict.clear(); Flask's session.clear() is the real equivalent (data only); fully dropping the session also takes response.delete_cookie(session_cookie_name).

Rust: at the language level HashMap::clear(); axum-session/actix-session expose purge and renew (matching destroy and regenerate respectively).

TypeScript: same as JavaScript.