Trying to delete specific cookies from google chrome

I would recommend you installing the sqlite3 command line interface and playing with SQL commands interactively there first. You are going to use two languages (SQL and Python) so you need to have some knowledge of both of them. Example of listing the database schema and first five host_keys:

$ sqlite3 '/mnt/c/Users/vaclav.brozik/AppData/Local/Google/Chrome/User Data/Default/Network/Cookies'
SQLite version 3.37.2 2022-01-06 13:25:41
Enter ".help" for usage hints.
sqlite> .schema
CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR);
CREATE TABLE cookies(creation_utc INTEGER NOT NULL,host_key TEXT NOT NULL,top_frame_site_key TEXT NOT NULL,name TEXT NOT NULL,value TEXT NOT NULL,encrypted_value BLOB NOT NULL,path TEXT NOT NULL,expires_utc INTEGER NOT NULL,is_secure INTEGER NOT NULL,is_httponly INTEGER NOT NULL,last_access_utc INTEGER NOT NULL,has_expires INTEGER NOT NULL,is_persistent INTEGER NOT NULL,priority INTEGER NOT NULL,samesite INTEGER NOT NULL,source_scheme INTEGER NOT NULL,source_port INTEGER NOT NULL,is_same_party INTEGER NOT NULL,last_update_utc INTEGER NOT NULL);
CREATE UNIQUE INDEX cookies_unique_index ON cookies(host_key, top_frame_site_key, name, path);
sqlite> SELECT DISTINCT host_key FROM cookies LIMIT 5 ;
.0272ac85-7522-8265-a555-397c3d825d95.prmutv.co
.0cf.io
.1dmp.io
.1rx.io
.360yield.com
sqlite>

Before the experiments terminate your Chrome (to have the database consistent) and copy the database to a new file to play with so you do not wreck you Chrome data.

Notice that in the sqlite3 CLI you need to terminate the queries using semicolon ; (not needed in the Python’s execute() method).

There are also GUI tools to play with SQL databases and queries. They may be easier to use but can be a little overwhelming at the beginning. This one works with SQLite: https://sqlitebrowser.org/

Here is an example code listing distinct host_keys in Python:

import sqlite3
import contextlib

database_file = '/mnt/c/Users/vaclav.brozik/AppData/Local/Google/Chrome/User Data/Default/Network/Cookies'

with contextlib.closing(sqlite3.connect(database_file)) as connection:
    cursor = connection.cursor()
    for (host_key,) in cursor.execute('SELECT DISTINCT host_key FROM cookies'):
        print(host_key)

The tricky part for you could be the unpacking in the for loop: for (host_key,) in [1]
When you iterate the cursor you are getting the individual rows. Every row is a tuple consisting of the values in individual columns. Python’s way to assign the values to individual variables is unpacking. In the example it is a single value assigned to the single variable host_key.

You can extend my example by adding commands inside the with block. When the block ends the database connection is automatically closed. Do not add new SQL queries inside the for loop - see below.

Currently I see these problems in your program:

  • cursor.execute("DELETE FROM cookies WHERE id=?", row)
    You try to filter by a column id but it looks like there is no such a column. I think you want to filter (and delete) by host_key.
  • This SQL query alone will do the work, there is no need to run it repeatedly in a loop (for) or conditionally using if:
    cursor.execute("DELETE FROM cookies WHERE host_key=?", (your_host_key,))
    Notice that we create a tuple for the parameters: (your_host_key,)
  • You process the cursor results in a for loop but inside the loop you execute another query using the (same!) cursor. This replaces the original cursor’s content. You cannot execute a new query this way when you intend to continue processing the previous query’s results.
  • Changes to the SQLite database must be committed using the commit() method before close() or the end of the with block.
  • You need to ensure that Chrome is closed so that you can modify the database without possible conflicts.

  1. It is possible to write just for host_key, in but I think that the single comma could be simply overlooked. ↩︎

2 Likes