Removing Cookies After User Opt-Out
If you are utilizing an opt-out consent model, or giving users the option to change their consent and opt-out of cookie categories at any point from a cookie settings link or button, you may want to remove cookies previously dropped on the user's browser for a category that they are now choosing to opt-out of.
Please note that this article is intended solely to offer you guidance on how to implement this deletion process if you decide to do so, and it does not constitute a recommendation from OneTrust in any form. It is essential to consider the norms and principles relevant to your circumstances when deciding whether to delete this data, and furthermore, to be transparent about your data handling practices.
To do this, you will need to implement the below function through the OptanonWrapper. This script will delete first-party cookies from the browser for a category that the user has opt-ed out of. It will also remove any local storage objects.
You will still need to implement blocking methods.
This is not functionality that the OneTrust tool provides. The following code is for example purposes only. OneTrust support cannot assist with implementing this functionality on a website.
function OptanonWrapper() {
// Get initial OnetrustActiveGroups ids
if(typeof OptanonWrapperCount == "undefined"){
otGetInitialGrps();
}
//Delete cookies
otDeleteCookie(otIniGrps);
// Assign OnetrustActiveGroups to custom variable
function otGetInitialGrps(){
OptanonWrapperCount = '';
otIniGrps = OnetrustActiveGroups;
// console.log("otGetInitialGrps", otIniGrps)
}
function otDeleteCookie(iniOptGrpId)
{
var otDomainGrps = JSON.parse(JSON.stringify(Optanon.GetDomainData().Groups));
var otDeletedGrpIds = otGetInactiveId(iniOptGrpId, OnetrustActiveGroups);
if(otDeletedGrpIds.length != 0 && otDomainGrps.length !=0){
for(var i=0; i < otDomainGrps.length; i++){
//Check if CustomGroupId matches
if(otDomainGrps[i]['CustomGroupId'] != '' && otDeletedGrpIds.includes(otDomainGrps[i]['CustomGroupId'])){
for(var j=0; j < otDomainGrps[i]['Cookies'].length; j++){
// console.log("otDeleteCookie",otDomainGrps[i]['Cookies'][j]['Name'])
//Delete cookie
eraseCookie(otDomainGrps[i]['Cookies'][j]['Name']);
}
}
//Check if Hostid matches
if(otDomainGrps[i]['Hosts'].length != 0){
for(var j=0; j < otDomainGrps[i]['Hosts'].length; j++){
//Check if HostId presents in the deleted list and cookie array is not blank
if(otDeletedGrpIds.includes(otDomainGrps[i]['Hosts'][j]['HostId']) && otDomainGrps[i]['Hosts'][j]['Cookies'].length !=0){
for(var k=0; k < otDomainGrps[i]['Hosts'][j]['Cookies'].length; k++){
//Delete cookie
eraseCookie(otDomainGrps[i]['Hosts'][j]['Cookies'][k]['Name']);
}
}
}
}
}
}
otGetInitialGrps(); //Reassign new group ids
}
//Get inactive ids
function otGetInactiveId(customIniId, otActiveGrp){
//Initial OnetrustActiveGroups
// console.log("otGetInactiveId",customIniId)
customIniId = customIniId.split(",");
customIniId = customIniId.filter(Boolean);
//After action OnetrustActiveGroups
otActiveGrp = otActiveGrp.split(",");
otActiveGrp = otActiveGrp.filter(Boolean);
var result=[];
for (var i=0; i < customIniId.length; i++){
if ( otActiveGrp.indexOf(customIniId[i]) <= -1 ){
result.push(customIniId[i]);
}
}
return result;
}
//Delete cookie
function eraseCookie(name) {
//Delete root path cookies
domainName = window.location.hostname;
document.cookie = name+'=; Max-Age=-99999999; Path=/;Domain='+ domainName;
document.cookie = name+'=; Max-Age=-99999999; Path=/;';
//Delete LSO incase LSO being used, cna be commented out.
localStorage.removeItem(name);
//Check for the current path of the page
pathArray = window.location.pathname.split('/');
//Loop through path hierarchy and delete potential cookies at each path.
for (var i=0; i < pathArray.length; i++){
if (pathArray[i]){
//Build the path string from the Path Array e.g /site/login
var currentPath = pathArray.slice(0,i+1).join('/');
document.cookie = name+'=; Max-Age=-99999999; Path=' + currentPath + ';Domain='+ domainName;
document.cookie = name+'=; Max-Age=-99999999; Path=' + currentPath + ';';
//Maybe path has a trailing slash!
document.cookie = name+'=; Max-Age=-99999999; Path=' + currentPath + '/;Domain='+ domainName;
document.cookie = name+'=; Max-Age=-99999999; Path=' + currentPath + '/;';
}
}
}
}
Make sure to test all behaviors and performance prior to implementing.