Skip to content

Cookie lifetimes: good practice

← Developers

Written for Cookie Rocks 2.12.2, checked on 20 November 2026. The current version is 2.12.3: some details may have changed.

How long a cookie lasts is set by your code, cut by the browser, and limited by how long the visitor’s yes lasts. Here is how to set it right, and how to tell Cookie Rocks. For the big picture, see How long cookies last.

Set the date the browser will keep

  • Prefer Max-Age, in seconds, to Expires: it does not depend on the visitor’s clock.
  • Never ask for more than 400 days. Chrome, Edge and Firefox cut any longer date to 400 days, Brave to 6 months.
  • Safari keeps a cookie written by a script (document.cookie) 7 days at most. A cookie your server sends with Set-Cookie keeps its date. If the cookie must last, set it from the server, with HttpOnly when no script needs to read it.
  • A session cookie has no date. It can still come back when the browser reopens the last tabs.
Set-Cookie: my_pref=compact; Max-Age=15552000; Path=/; Secure; SameSite=Lax; HttpOnly

Do not write it again without a yes

Many tools write their cookie again at each visit, with a new date. That is fine while the visitor’s yes is valid. When the yes has ended, or was never given, ask Cookie Rocks first.

function whenCookieRocks(callback) {
	if (window.CookieRocks) {
		callback();
	} else {
		document.addEventListener('cookie-rocks:ready', callback, { once: true });
	}
}

whenCookieRocks(function () {
	CookieRocks.onConsent('statistics', function () {
		// Only now: write or refresh your statistics cookie.
	});
});

Delete it when the yes is taken back

Cookie Rocks deletes the cookies and storage it knows when a visitor takes back a yes, then reloads the page. For a cookie of your own, delete it yourself, with the same name, path and domain it was set with.

document.addEventListener('cookie-rocks:change', function (event) {
	if (!event.detail.consent.statistics) {
		document.cookie = 'my_stats_id=; Max-Age=0; Path=/; Secure; SameSite=Lax';
		localStorage.removeItem('my_stats_cache');
	}
});

Give browser storage a date of your own

Local storage never expires by itself. Store the time you wrote the value, and drop it when it is too old.

const MAX = 180 * 24 * 3600 * 1000; // 180 days
const saved = JSON.parse(localStorage.getItem('my_pref') || 'null');
if (saved && Date.now() - saved.t > MAX) {
	localStorage.removeItem('my_pref');
}
localStorage.setItem('my_pref', JSON.stringify({ v: 'compact', t: Date.now() }));

Tell Cookie Rocks how long your cookies last

Declare your tool with the cookie_rocks_services filter. Each cookie is a pair of name and duration, the one your code really sets: it appears in the visitor’s banner and in your cookie policy. See PHP filters, actions and REST API.

add_filter( 'cookie_rocks_services', function ( $services ) {
	$services['my-stats'] = array(
		'name'     => 'My Stats',
		'provider' => '',
		'category' => 'statistics',
		'patterns' => array( 'my-stats.js' ),
		'purpose'  => 'analytics',
		'cookies'  => array( array( 'my_stats_id', '6mo' ) ),
	);
	return $services;
} );

Check what the browser really keeps

  • In Chrome or Edge, open the developer tools, Application, Cookies: the Expires / Max-Age column shows the date the browser kept, already cut to 400 days.
  • Test in Safari too: a date written by a script shows 7 days there.
  • The free Audit lists the cookies your pages set, so you can compare.
You make a plugin or a theme?Check your cookie and storage names before you ship. A name already taken, or too generic, gets sorted and blocked with someone else’s.
For plugin and theme developers →