Back
deleteAll()
Deletes all records that match the where
argument.
By default, objects will not be instantiated and therefore callbacks and validations are not invoked.
You can change this behavior by passing in instantiate=true
.
Returns the number of records that were deleted.
Example 1: Delete inactive users (skip callbacks and validations)
<cfscript>
recordsDeleted = model("user").deleteAll(where="inactive=1", instantiate=false);
writeOutput("Deleted #recordsDeleted# inactive users.");
</cfscript>
Deletes all users where inactive=1.
Objects are not instantiated, so callbacks and validations are skipped.
Example 2: Scoped delete using an association
<cfscript>
post = model("post").findByKey(params.postId);
// Deletes all comments associated with this post
howManyDeleted = post.deleteAllComments();
writeOutput("Deleted #howManyDeleted# comments for this post.");
</cfscript>
Assumes a hasMany association from post → comment.
Internally calls model("comment").deleteAll(where="postId=#post.id#").
Example 3: Delete and run callbacks
<cfscript>
recordsDeleted = model("user").deleteAll(where="inactive=1", instantiate=true, callbacks=true);
</cfscript>
Deletes the records after instantiating the objects.
Any beforeDelete or afterDelete callbacks are triggered.
Copied!