diff --git a/clickhouse/config.xml b/clickhouse/config.xml
index b253536c01b..6127385fb85 100644
--- a/clickhouse/config.xml
+++ b/clickhouse/config.xml
@@ -7,6 +7,7 @@
+
diff --git a/install.sh b/install.sh
index fb41671798e..9d3924d645a 100755
--- a/install.sh
+++ b/install.sh
@@ -28,6 +28,7 @@ source install/check-minimum-requirements.sh
# Upgrading clickhouse needs to come first before turning things off, since we need the old clickhouse image
# in order to determine whether or not the clickhouse version needs to be upgraded.
source install/upgrade-clickhouse.sh
+source install/cleanup-clickhouse.sh
source install/update-docker-images.sh
source install/turn-things-off.sh
source install/create-docker-volumes.sh
diff --git a/install/cleanup-clickhouse.sh b/install/cleanup-clickhouse.sh
new file mode 100644
index 00000000000..bbf7e8c2fc7
--- /dev/null
+++ b/install/cleanup-clickhouse.sh
@@ -0,0 +1,39 @@
+echo "${_group}Cleaning up ClickHouse query log tables ..."
+
+if [ "$CONTAINER_ENGINE" = "podman" ]; then
+ ps_command="$dc ps"
+else
+ ps_command="$dc ps -a"
+fi
+
+drop_clickhouse_system_table_if_exists() {
+ local table="$1"
+ local table_exists
+
+ table_exists=$($dc exec clickhouse clickhouse-client -q "SELECT count() FROM system.tables WHERE database = 'system' AND name = '$table'")
+
+ if [[ "$table_exists" == "1" ]]; then
+ echo "Dropping system.$table ..."
+ $dc exec clickhouse clickhouse-client -q "DROP TABLE system.$table"
+ else
+ echo "system.$table does not exist, skipping."
+ fi
+}
+
+truncate_clickhouse_query_log_tables() {
+ echo "Truncating system.query_log and system.query_views_log ..."
+ $dc exec clickhouse clickhouse-client --multiquery -q "
+SYSTEM FLUSH LOGS;
+TRUNCATE TABLE IF EXISTS system.query_log;
+TRUNCATE TABLE IF EXISTS system.query_views_log;
+"
+}
+
+if $ps_command | grep -q clickhouse; then
+ start_service_and_wait_ready clickhouse
+ drop_clickhouse_system_table_if_exists query_log_0
+ drop_clickhouse_system_table_if_exists query_views_log_0
+ truncate_clickhouse_query_log_tables
+fi
+
+echo "${_endgroup}"