Shrink database size

I’ve been using Mermost 5.12.1 for more than a year, about 300+ active users. The size of the postgre DB is 41GB. Tell me how can I clean / shrink the database? Messages older than 3-4 months I do not need.
CentOS 7

[root@mm ~]# du -sh /opt/mattermost/*
8.0K /opt/mattermost/app
92K /opt/mattermost/cert
40K /opt/mattermost/contrib
12K /opt/mattermost/db
4.0K /opt/mattermost/docker-compose.yml
3.1M /opt/mattermost/ldap
12K /opt/mattermost/LICENSE
4.0K /opt/mattermost/
1.2G /opt/mattermost/mattermost
41G /opt/mattermost/postgresql
12K /opt/mattermost/

Hi there, @gvolshebnik

First things first, I would recommend you to consider backing up and upgrading Mattermost to the latest supported version, which is 5.24.2 to ensure that you are up to date with the latest features, security and bug fixes.

Moving back to the process of shrinking the database, the Data Retention Policy which is available for E20 offering is the best way to go for long term solution to ensure that you only keep what you need. May I know if it would be something that you would consider?

Hi @ahmaddanial
Thank you for your answer
E20 is not the best way out for us due to its high cost. The administrator’s manual says “do not touch the DB directly, use CLI”, but I did not find anything related to deleting messages in the CLI manual. Is there any other way?

Hello, @gvolshebnik

You are welcome. So, there is no default way of deleting messages / posts using command line.

However, I came across an old post that explains the steps to delete old messages in this post.

I recommend you to attempt this on a staging / development instance first before applying them on the production to avoid unwanted data loss.