Back
afterDelete()
Registers one or more callback methods that should be executed after an object is deleted from the database. This hook allows you to perform cleanup, logging, or side effects when a record has been removed.
1. Single callback method
// Call `logDeletion` after an object is deleted
afterDelete("logDeletion");
function logDeletion() {
writeLog("Record deleted at #now()#");
}
2. Multiple callbacks
afterDelete("archiveData,notifyAdmin");
function archiveData() {
// move deleted data to an archive table
}
function notifyAdmin() {
// send a notification email
}
3. With related cleanup
afterDelete("removeAssociatedRecords");
function removeAssociatedRecords() {
// remove orphaned child records manually
Order.deleteAll(where="userId = #this.id#");
}
4. Practical usage in User.cfc
component extends="Model" {
function config() {
afterDelete("cleanupSessions,sendGoodbyeEmail");
}
function cleanupSessions() {
Session.deleteAll(where="userId = #id#");
}
function sendGoodbyeEmail() {
// code to send a farewell email
}
}
Copied!