session_destroy

session_destroy(): bool

Destroys the session data in storage (logout semantics); it does not clear the $_SESSION superglobal — clear it yourself.

C C23

C23
#include <stdio.h>
#include <string.h>

struct KV {
    char key[32];
    char val[64];
};

static const char *kv_get(struct KV *store, int n, const char *key) {
    for (int i = 0; i < n; i++) {
        if (strcmp(store[i].key, key) == 0) return store[i].val;
    }
    return NULL;
}

int main(void) {
    /* Counterpart semantics: destroy the stored data (counter zeroed); reopening starts empty */
    struct KV store[2];
    int n = 1;
    strcpy(store[0].key, "token");
    strcpy(store[0].val, "abc");
    printf("token: %s\n", kv_get(store, n, "token"));
    n = 0; /* Destroy */
    printf("destroyed: %s\n", n == 0 ? "yes" : "no");
    printf("reopened: %s\n", n == 0 ? "empty" : "stale");
    return 0;
}
Output
token: abc
destroyed: yes
reopened: empty

C++ C++26

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

int main() {
    // Counterpart semantics: destroy the stored data; reopening starts empty
    std::map<std::string, std::string> store{{"token", "abc"}};
    std::cout << "token: " << store["token"] << "\n";
    store.clear();
    std::cout << "destroyed: " << (store.empty() ? "yes" : "no") << "\n";
    std::cout << "reopened: " << (store.empty() ? "empty" : "stale") << "\n";
    return 0;
}
Output
token: abc
destroyed: yes
reopened: empty

C# v11.0.100

v11.0.100
// Counterpart semantics: destroy the stored data; reopening starts empty
var store = new Dictionary<string, string> { ["token"] = "abc" };
Console.WriteLine($"token: {store["token"]}");
store.Clear();
Console.WriteLine($"destroyed: {(store.Count == 0 ? "yes" : "no")}");
Console.WriteLine($"reopened: {(store.Count == 0 ? "empty" : "stale")}");
Output
token: abc
destroyed: yes
reopened: empty

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// Counterpart semantics: destroy the stored data; reopening starts empty
	store := map[string]string{"token": "abc"}
	fmt.Println("token:", store["token"])
	store = map[string]string{}
	fmt.Println("destroyed:", map[bool]string{true: "yes", false: "no"}[len(store) == 0])
	fmt.Println("reopened:", map[bool]string{true: "empty", false: "stale"}[len(store) == 0])
}
Output
token: abc
destroyed: yes
reopened: empty

Java v27.0.1

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

public class Main {
    public static void main(String[] args) {
        // Counterpart semantics: destroy the stored data; reopening starts empty (session.invalidate() in Servlet terms)
        Map<String, String> store = new HashMap<>();
        store.put("token", "abc");
        System.out.println("token: " + store.get("token"));
        store.clear();
        System.out.println("destroyed: " + (store.isEmpty() ? "yes" : "no"));
        System.out.println("reopened: " + (store.isEmpty() ? "empty" : "stale"));
    }
}
Output
token: abc
destroyed: yes
reopened: empty

JavaScript v26.13.1

v26.13.1
// Counterpart semantics: destroy the stored data; reopening starts empty
const store = new Map([["token", "abc"]]);
console.log(`token: ${store.get("token")}`);
store.clear();
console.log(`destroyed: ${store.size === 0 ? "yes" : "no"}`);
console.log(`reopened: ${store.size === 0 ? "empty" : "stale"}`);
Output
token: abc
destroyed: yes
reopened: empty

PHP v8.6.0

v8.6.0
// session_destroy: deletes the session data in storage (logout semantics)
ini_set("session.save_path", sys_get_temp_dir());
session_start();
$_SESSION["token"] = "abc";
echo $_SESSION["token"], "\n";
session_write_close();
session_start();
echo $_SESSION["token"], "\n";   // Reopening: the data is still there
// The official logout recipe: empty the superglobal + destroy storage (destroy does not clear $_SESSION for you)
$_SESSION = [];
session_destroy();
echo session_status() === PHP_SESSION_ACTIVE ? "active\n" : "inactive\n";
session_start();
echo isset($_SESSION["token"]) ? "kept\n" : "cleared\n";
session_destroy();
Output
abc
abc
inactive
cleared

Python v3.15.2

v3.15.2
# Counterpart semantics: destroy the stored data; reopening starts empty
store = {"token": "abc"}
print("token:", store["token"])
store.clear()
print("destroyed:", "yes" if not store else "no")
print("reopened:", "empty" if not store else "stale")
Output
token: abc
destroyed: yes
reopened: empty

Rust v1.99.1

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

fn main() {
    // Counterpart semantics: destroy the stored data; reopening starts empty
    let mut store = HashMap::new();
    store.insert("token", "abc");
    println!("token: {}", store["token"]);
    store.clear();
    println!("destroyed: {}", if store.is_empty() { "yes" } else { "no" });
    println!("reopened: {}", if store.is_empty() { "empty" } else { "stale" });
}
Output
token: abc
destroyed: yes
reopened: empty

TypeScript v26.13.1

v26.13.1
// Counterpart semantics: destroy the stored data; reopening starts empty
const store = new Map<string, string>([["token", "abc"]]);
console.log(`token: ${store.get("token")}`);
store.clear();
console.log(`destroyed: ${store.size === 0 ? "yes" : "no"}`);
console.log(`reopened: ${store.size === 0 ? "empty" : "stale"}`);
Output
token: abc
destroyed: yes
reopened: empty

Language differences

C: destroy means zeroing the count/resetting the struct; a real logout also has to clear the browser cookie (hand-send an expired Set-Cookie).

C++: same as C — clear() is the destroy.

C#: Dictionary.Clear() corresponds to destroying; in ASP.NET it's HttpContext.SignOutAsync()/Session.Clear(), with the cookie handled by the framework.

Go: rebuild an empty map; in gorilla/sessions it's session.Options.MaxAge = -1 plus session.Save (making the browser drop the cookie).

Java: Map.clear() corresponds to destroying; in Servlets it's session.invalidate() — the container invalidates the session ID too.

JavaScript: Map.clear() corresponds to destroying; in express-session it's req.session.destroy(cb) (server-side storage removal), with res.clearCookie for the browser cookie.

PHP: session_destroy deletes only the stored session data — three things it does not do: it doesn't clear $_SESSION (do $_SESSION = [] yourself), doesn't remove the session cookie (the browser re-opens a fresh session with the old ID), and doesn't touch other sessions; the full logout idiom is unset/clear + destroy + (optionally) cookie removal.

Python: dict.clear() corresponds to destroying; in Flask it's session.clear() (signed-cookie scheme where the client holds the storage).

Rust: HashMap::clear corresponds to destroying.

TypeScript: same as JavaScript.