wp db size
Overview
Show how much disk space the WordPress database uses — either the total DB size or broken down by individual tables. The go-to command for spotting database bloat before it causes performance problems.
What It Does
wp db size queries information_schema to report the size (data + index) of the WordPress database. You can view data at the database level or per table, in human-readable or machine-readable formats.
Syntax
wp db size [--size_format=<format>] [--tables] [--format=<format>] [--scope=<scope>]
Options & Flags
| Flag | Default | Description |
|---|---|---|
--size_format=FORMAT | b (bytes) | Unit: b (bytes), kb, mb, gb |
--tables | — | Show size for each table individually |
--format=FORMAT | table | Output format: table, json, csv, yaml |
--scope=SCOPE | all | Filter scope: all, global, blog, old |
--human-readable | — | Show sizes in human-readable format |
Basic Usage
Total database size
wp db size
Output:
+------------+------------+
| Name | Size |
+------------+------------+
| wordpress | 45,678,912 |
+------------+------------+
Human-readable total
wp db size --size_format=mb
Output:
+------------+--------+
| Name | Size |
+------------+--------+
| wordpress | 43.56 |
+------------+--------+
Size by table
wp db size --tables --size_format=mb
Output (truncated):
+---------------------+--------+
| Name | Size |
+---------------------+--------+
| wp_options | 18.23 |
| wp_postmeta | 9.45 |
| wp_posts | 6.12 |
| wp_redirection_logs | 4.87 |
| wp_comments | 1.23 |
+---------------------+--------+
Real-World Scenarios
Scenario 1: Identify top 5 largest tables
wp db size --tables --size_format=mb --format=csv | sort -t',' -k2 -rn | head -6
Scenario 2: Monitor DB size in a script
DB_SIZE=$(wp db size --size_format=mb --format=csv | tail -1 | cut -d',' -f2)
echo "Database size: ${DB_SIZE} MB"
if (( $(echo "${DB_SIZE} > 500" | bc -l) )); then
echo "WARNING: Database exceeds 500 MB — consider optimization"
fi
Scenario 3: Pre and post optimization comparison
echo "Before optimization:"
wp db size --size_format=mb
wp db optimize
echo "After optimization:"
wp db size --size_format=mb
Scenario 4: Report per-table sizes for client audit
wp db size --tables --size_format=mb --format=csv > db_size_report_$(date +%Y%m%d).csv
cat db_size_report_$(date +%Y%m%d).csv
Common Bloat Causes
| Table | Common Cause of Bloat |
|---|---|
wp_options | Plugins storing data with autoload=yes; transients not cleaned |
wp_postmeta | Orphaned post meta; page builders storing large JSON |
wp_redirection_logs | Redirection plugin logs not rotated |
wp_woocommerce_sessions | Customer session data not purged |
wp_comments | Spam comments not deleted |
wp_actionscheduler_* | WooCommerce scheduled actions accumulating |
Best Practices
- Check DB size monthly to catch unexpected growth early.
- Use
--tablesto identify the specific source of bloat. - Pair with
wp db optimizeafter cleaning large tables. - Delete plugin data from deactivated plugins — they often leave orphaned tables.
- Purge transients regularly with
wp transient delete --expired.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| Reported size is much smaller than actual | InnoDB fragmentation | Run wp db optimize to reclaim space |
| Unknown tables in output | Old plugin tables not cleaned up | Remove manually with wp db query "DROP TABLE ..." |
Access denied on information_schema | DB user lacks SELECT on information_schema | Grant access or use root credentials |
Quick Reference
wp db size # Total DB size in bytes
wp db size --size_format=mb # Total in MB
wp db size --tables --size_format=mb # Per-table in MB
wp db size --tables --format=csv # CSV for spreadsheet
Next Steps
wp db optimize— reclaim space and reduce fragmentation.wp transient delete --expired— clean transients that bloatwp_options.wp db query— run custom queries to investigate specific tables.