Resource Usage & CloudLinux Limits

Managing High Disk Usage of MySQL on cPanel Servers: A Comprehensive Guide

By the Domain India teamPublished 7 min read
Knowledge base article
Contents (9 sections)

When a server's disk fills up, the MySQL or MariaDB data directory is often the reason: a few large tables, binary logs nobody expires, or a shared tablespace that grew once and never shrank. This guide shows how to find what is using the space and how to reclaim it safely, without deleting files that the database needs to start.

This guide is for your own server

The command-line steps here need root access. They apply to a VPS or dedicated server you administer, with or without cPanel. On Domain India shared hosting you cannot see /var/lib/mysql or change server settings; section 1 shows what you can do from your control panel instead.

Key takeaways

Measure first: query information_schema for the largest databases and tables, and check the binary logs. Expire binary logs with PURGE BINARY LOGS and a retention setting, never with rm. Reclaim space inside tables with OPTIMIZE TABLE after large deletes, and delete big batches of rows in chunks. The shared ibdata1 file never shrinks on its own; the only fix is dump, rebuild and reload. Never delete ibdata1, the redo logs or files in the data directory by hand.

1. On shared hosting: what you can do

If your hosting account is near its disk limit and a database is the cause, work inside your own databases:

  1. See which database is large.
    cPanel's Manage My Databases page lists each database with its size. phpMyAdmin shows the size of every table in the database's structure view.
  2. Find the heavy tables.
    In WordPress, the usual culprits are log tables written by security, redirection or statistics plugins, old post revisions, expired transients in wp_options, and abandoned WooCommerce sessions.
  3. Clean up at the source.
    Lower the plugin's log retention, or remove a plugin you no longer use along with its tables. Take a backup first.
  4. Reclaim the space.
    After deleting a lot of rows, select the table in phpMyAdmin and choose Optimize table.

For how disk and other limits are measured on your plan, see Check hosting resource usage. If you can't tell what is using the space, open a support ticket.

2. Measure: which databases and tables are largest

On your own server, ask the database rather than the file system. This lists databases by size:

sql
SELECT table_schema AS db,
       ROUND(SUM(data_length + index_length) / 1024 / 1024) AS size_mb,
       ROUND(SUM(data_free) / 1024 / 1024) AS free_mb
FROM information_schema.TABLES
GROUP BY table_schema
ORDER BY size_mb DESC
LIMIT 20;

Change GROUP BY table_schema to list table_schema, table_name for the largest tables. free_mb is space allocated inside the table files but not in use; a large value means OPTIMIZE TABLE would give space back.

Then compare with the files on disk:

bash
sudo du -sh /var/lib/mysql/* | sort -rh | head -20
sudo df -h /var/lib/mysql

If du shows much more than the query, the difference is usually binary logs, ibdata1, the temporary tablespace or logs, covered below. Check the actual data directory with SELECT @@datadir;, as it is not always /var/lib/mysql.

3. Common causes

WhatWhere it livesSafe way to reclaim
Binary logsbinlog files named in SHOW BINARY LOGSPURGE BINARY LOGS and a retention setting
Large or bloated tablesOne .ibd file per tableDelete old rows in batches, then OPTIMIZE TABLE
Shared system tablespaceibdata1Dump, rebuild and reload; never delete
Temporary tablespaceibtmp1Shrinks when the service restarts
General or slow query logFiles set by general_log_file and slow_query_log_fileTurn the log off or rotate it
Leftover backupsDump files saved on the same diskMove them off the server

4. Binary logs: expire them properly

Binary logs record every change for replication and point-in-time recovery. Without an expiry setting they grow until the disk is full. Check them:

sql
SHOW BINARY LOGS;

Remove old ones from inside the server, which keeps the index file consistent:

sql
PURGE BINARY LOGS BEFORE NOW() - INTERVAL 7 DAY;

Then set a retention period in the server's configuration, under [mysqld], and restart:

ini
binlog_expire_logs_seconds = 604800

That keeps seven days. Older MariaDB versions use expire_logs_days = 7 instead; check your version with SELECT VERSION();. If the server is a replication source, keep logs until every replica has read them. If you don't use replication or point-in-time recovery, you can turn binary logging off, but understand what you lose first.

Never delete database files with rm

Old guides suggest rm -rf /var/lib/mysql/.log, or deleting ibdata1, ib_logfile or undo files. Deleting files under the data directory can stop the server from starting and make every InnoDB table unreadable. Deleting a log file the server still has open doesn't even free the space. Use PURGE BINARY LOGS, turn logs off in the configuration, or rebuild from a dump.

5. Tables that grew too large

Most runaway growth comes from log, session or queue tables that an application writes to and never cleans. Fix the application's retention first, or the table fills up again.

To delete millions of old rows, work in batches so the undo log and replication stay small:

sql
DELETE FROM app_log
WHERE created_at < NOW() - INTERVAL 90 DAY
LIMIT 10000;

Repeat until it reports 0 rows affected. A single huge DELETE holds locks for a long time and can itself use a lot of disk while it runs. If you want to empty a table completely, TRUNCATE TABLE app_log; is instant and releases the space.

Deleting rows doesn't shrink the file. After a large clean-up, rebuild the table:

sql
OPTIMIZE TABLE app_log;

On InnoDB this recreates the table and needs free disk space roughly the size of the table while it runs. Run it after big deletes, not on a schedule.

6. The ibdata1 file

With innodb_file_per_table on, which is the default in current MySQL and MariaDB, each table has its own .ibd file and ibdata1 stays small. A large ibdata1 usually means tables were created before that setting was on, or it grew during a very long transaction. It never shrinks by itself.

To move a table out of ibdata1, rebuild it while innodb_file_per_table is on:

sql
ALTER TABLE your_table ENGINE=InnoDB;

That moves the data but doesn't shrink ibdata1. The only way to shrink it is to dump every database, stop the server, re-create the data directory and reload the dump. Plan that as a maintenance window, with a tested backup, or ask a database administrator.

7. Temporary tables and logs

  • Temporary tablespace. InnoDB's ibtmp1 grows during large sorts and joins and is re-created at its normal size when the service restarts. If it keeps growing, find the query behind it in the slow query log.
  • Orphaned #sql- files. These are left by an interrupted ALTER TABLE. Don't delete them by hand; check the server's documentation for your version.
  • General query log. It records every query and grows fast. Keep general_log = 0 except for short debugging sessions.
  • Error and slow query logs. Rotate them with the logrotate configuration your package installs, and keep long_query_time sensible so the slow log only records slow queries.

8. Prevent it happening again

Watch disk space
Alert when the data partition passes about 80%, so you act before writes fail.
Set log retention
Give binary logs an expiry and keep the general log off.
Fix application retention
Make plugins and apps delete their own old logs and sessions.
Keep backups elsewhere
Store dumps and backups off the database disk, ideally off the server.

A full disk while the database is writing is a common cause of crashed tables. If that has already happened, see Checking, repairing and optimising MySQL and MariaDB databases.

9. Where Domain India fits

On Domain India shared hosting, the database server is managed for you; you look after your own databases from phpMyAdmin, and included backups are weekly with JetBackup 5 on cPanel and DirectAdmin. If you need root access to tune or clean the database server yourself, a Domain India VPS is self-managed with full root access.

VPS Starter
₹552.65/mo + GST
  • 1 vCPU
  • 2 GB DDR4 RAM
  • 64 GB NVMe SSD Storage
  • 2 TB Monthly Bandwidth
See plan details

The card shows the live Domain India price, excluding 18% GST.

Is it safe to delete files in /var/lib/mysql to free space?

No. Deleting ibdata1, redo logs, undo files or binary logs by hand can stop the server from starting or make tables unreadable. Use PURGE BINARY LOGS, OPTIMIZE TABLE after large deletes, or a dump and reload.

How do I find which MySQL database is using the most disk space?

Query information_schema.TABLES and sum data_length and index_length per table_schema, ordered by size. On shared hosting, cPanel's Manage My Databases page and phpMyAdmin show database and table sizes.

Why didn't deleting rows free any disk space?

InnoDB keeps the freed space inside the table file for reuse. Run OPTIMIZE TABLE on the table to rebuild it and return the space. It needs free disk space about the size of the table while it runs.

How do I stop binary logs filling the disk?

Purge old logs with PURGE BINARY LOGS BEFORE a date, then set binlog_expire_logs_seconds (or expire_logs_days on older MariaDB) in the server configuration so old logs are removed automatically.

Can I shrink ibdata1?

Not in place. Rebuilding tables with innodb_file_per_table on moves their data out, but ibdata1 only gets smaller if you dump all databases, re-create the data directory and reload the dump.

Can I change MySQL server settings on Domain India shared hosting?

No. Server settings are shared by every account on the server. You can clean and optimise your own tables in phpMyAdmin. For full control, use a VPS.

Ready to take control of your database server? Compare VPS plans, or, on shared hosting, start with managing your database in phpMyAdmin. Not sure what is filling your account? Open a support ticket.

Need root access to your database server?

Self-managed VPS plans with full root access on KVM virtualisation.

See VPS plans

Ready when you are

Get cPanel hosting from ₹125/mo + GST

See plans

Was this article helpful?

Your answer helps us decide what to improve next.

Still need help? Open a support ticket and our team will reply.

Prefer an app? Add this site to your home screen.Get the app