Đôi khi bạn muốn xóa cookie để các lần đọc sau trả về rỗng. Chỉ cần đặt ngày hết hạn về quá khứ.
Ví dụ: Xóa cookie bằng cách set expiry về 1 tháng trước
<html>
<head>
<script>
function WriteCookie() {
var now = new Date();
now.setMonth(now.getMonth() - 1);
cookievalue = escape(document.myform.customer.value) + ";";
document.cookie = "name=" + cookievalue;
document.cookie = "expires=" + now.toUTCString() + ";";
document.write("Setting Cookies: " + "name=" + cookievalue);
}
</script>
</head>
<body>
<form name="myform" action="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onclick="WriteCookie()"/>
</form>
</body>
</html>
Cách xóa chuẩn: document.cookie = "name=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;" (phải khớp path và domain lúc tạo).