Plausible Analytics Takes Up Too Much Space - How To Fix

August 18th 2022/Share/Edit

I've been running Plausible for 8 months or so and noticed that the clickhouse database that it requires to run was 30+ GB in size (and this site isn't that popular).

Doing some digging took me to this useful Github thread.

The basics are as follows!

Clearing The Logs

By Default Clickhouse outputs and stores a lot of logs.

We'll first want to clean them up.

If you're using Docker, find your containers ID by using:

docker ps

and then enter the container's shell via:

docker exec -it <CONTAINER_ID> bash

and then run this command to clear the logs:

clickhouse-client -q "SELECT name FROM system.tables WHERE name LIKE '%log%';" | xargs -I{} clickhouse-client -q "TRUNCATE TABLE system.{};"

Great! After that the files went from 30GB in size to 201MB!

Configuring Clickhouse To Omit Unneeded Logs

Let's first create the necessary config files:

clickhouse-config.xml:

<yandex>
    <logger>
        <level>warning</level>
        <console>true</console>
    </logger>

    <!-- Stop all the unnecessary logging -->
    <query_thread_log remove="remove"/>
    <query_log remove="remove"/>
    <text_log remove="remove"/>
    <trace_log remove="remove"/>
    <metric_log remove="remove"/>
    <asynchronous_metric_log remove="remove"/>
</yandex>

clickhouse-user-config.xml:

<yandex>
    <profiles>
        <default>
            <log_queries>0</log_queries>
            <log_query_threads>0</log_query_threads>
        </default>
    </profiles>
</yandex>

And then map the volumes to these files in your docker-compose file:

image: yandex/clickhouse-server:latest
volumes:
    - ./clickhouse/clickhouse-config.xml:/etc/clickhouse-server/config.d/docker_related_config.xml:ro
    - ./clickhouse/clickhouse-user-config.xml:/etc/clickhouse-server/users.d/docker_related_user_config.xml:ro