wiki:VCLExampleRemovingSomeCookies

Version 2 (modified by zviratko, 4 years ago) (diff)

better version for removing cookies, and a nasty hack for removing all but some cookies

Removing all, but not all cookies

In some cases, you might want to remove only a few, selected cookies, for example if you use Google Analytics.

Currently, this has to be done using regular expressions as follows:

sub vcl_recv {
    # Is it the first one?
    set req.http.cookie = regsub(req.http.cookie, "foo=[^;]+(; )?", "");

    # Or perhaps one in the middle or the last one?
    set req.http.cookie = regsub(req.http.cookie, "(; )?foo=[^;]+", "");
    if (req.http.cookie ~ "^ *$") {
        remove req.http.cookie;
    }
}

Replace foo with the name of the cookie you wish to remove.

Another version for removing some cookies

sub vcl_recv {
if (req.http.Cookie) {
    set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *__utm.=[^;]+;? *", "\1"); # removes all cookies named __utm? (utma, utmb...) - tracking thing

    if (req.http.Cookie == "") {
        remove req.http.Cookie;
    }
}

Removing all BUT some cookies

(warning - not pretty)

removes all cookies but the ones named COOKIE1 and COOKIE2

sub vcl_recv {
if (req.http.Cookie) {
    set req.http.Cookie = regsuball(req.http.Cookie, "(^|;) +", "\1");
    set req.http.Cookie = regsuball(req.http.Cookie, "(^|;)(COOKIE1|COOKIE2)=", "; \2=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsub(req.http.Cookie, "^[; ]*|[; ]*$", "");

    if (req.http.Cookie == "") {
        remove req.http.Cookie;
    }
}