db:delete
This command can be used, to reduce database size by deleting records according to a condition. For instance, removing wp_posts revisions.
Config file
The config for this command goes under cleanup.delete
section. It is an array
of operations. Each operation is related to a table. the fields are the following :
- table : table name
- multi : For wordpress multi-instance. (see. db:truncate)
- conditions : An array of AND conditions. Each condition is :
- field : string representing a table column
- operator : SQL operator (<, >, <>, etc ...)
- value : value on which the field will be compared against
Behind the scences, conditions will be passed to Laravel / Query builder where
clause.
<?php
foreach ($tableOperations->conditions as $cond) {
$dbTable->where($cond->field, $cond->operator, $cond->value);
}
Config example
"cleanup": {
"delete": [
{
"table": "posts",
"multi": true,
"conditions": [
{
"field": "post_type",
"operator": "!=",
"value": "unkown_type"
},
{
"field": "post_date",
"operator": "<",
"value": "2025"
}
]
},
{
"table": "wp_comments",
"multi": false,
"conditions": [
{
"field": "comment_date",
"operator": "<",
"value": "2025"
}
]
}
]
}
Command
appcli db:delete /path/to/config-file.json
Result example
Deleting items from table wp_1000_posts ...
-> [{"field":"post_type","operator":"!=","value":"unkown_type"},{"field":"post_date","operator":"<","value":"2025"}]
Deleting items from table wp_posts ...
-> [{"field":"post_type","operator":"!=","value":"unkown_type"},{"field":"post_date","operator":"<","value":"2025"}]
Deleting items from table wp_comments ...
-> [{"field":"comment_date","operator":"<","value":"2025"}]
Database size: before=1Mb -> after=1Mb > Gain : 0Mb (0%)
Took about 0 min. ( 0 sec.)