Removing Objects
Last updated Dec 21st, 2019 | Page history | Improve this page | Report an issue
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$306 per month—let's make that $500!
Learn morexPDOObject.remove()¶
When you 'remove' an Object in xPDO, you delete its row from the database. xPDO abstracts this out into a remove function, seen here:
$box = $xpdo->getObject('Box',134);
if ($box->remove() == false) {
echo 'An error occurred while trying to remove the box!';
}
The remove function will return either true or false, depending on the outcome of the deletion. Errors will also be logged via $xpdo->log.
This will also remove any composite related objects to this object. For example, if our Box had 4 "side" related objects that were mapped out as composites, they would be removed as well when $box->remove is called.
xPDO.removeCollection($class, $criteria)¶
This method is used to delete multiple objects.
http://api.modxcms.com/xpdo/xPDO.html#removeCollection
Examples¶
From modSessionHandler:
public function gc($max) {
$max = (integer) $this->modx->getOption('session_gc_maxlifetime',null,$max);
$maxtime= time() - $max;
$result = $this->modx->removeCollection('modSession', array("`access` < {$maxtime}"));
return $result;
}
Warning Careful! If you do not specify your criteria correctly, you can wipe out an entire database table!
Both parameters required¶
Note that the both parameters (object type and the array of selectors) are required. If you want to delete everything in a table, pass 'array()' as the second parameter and this will match and delete all items.
For example, to delete all objects of type 'objectName' from the database, do the following.
$modx->removeCollection('objectName', array());
Does not remove composites¶
When using removeCollection()
, composite related objects are not automatically removed, unlike the remove() function.
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$306 per month—let's make that $500!
Learn more