Skip to content

Model Class

deleteByKey()

deleteByKey() — returns boolean

Available in: model Category: Delete Functions

Finds the record with the supplied key and deletes it. Returns true on successful deletion of the row, false otherwise.

NameTypeRequiredDefaultDescription
keyanyyesPrimary key value(s) of the record to fetch. Separate with comma if passing in multiple primary key values. Accepts a string, list, or a numeric value.
reloadbooleannofalseSet to true to force Wheels to query the database even though an identical query for this model may have been run in the same request. (The default in Wheels is to get the second query from the model’s request-level cache.)
transactionstringno[runtime expression]Set this to commit to update the database, rollback to run all the database queries but not commit them, or none to skip transaction handling altogether.
callbacksbooleannotrueSet to false to disable callbacks for this method.
includeSoftDeletesbooleannofalseSet to true to include soft-deleted records in the queries that this method runs.
softDeletebooleannotrueSet to false to permanently delete a record, even if it has a soft delete column.
Example 1: Delete a user by primary key
<cfscript>
result = model("user").deleteByKey(1);

if (result) {
    writeOutput("User deleted successfully.");
} else {
    writeOutput("Failed to delete user.");
}
</cfscript>

Deletes the user with id=1.

Returns true on success, false on failure.

Example 2: Delete a record permanently (ignore soft delete)
<cfscript>
result = model("user").deleteByKey(1, softDelete=false);

if (result) {
    writeOutput("User permanently deleted.");
}
</cfscript>

Ignores any soft delete column.

The record is removed from the database entirely.

Example 3: Disable callbacks
<cfscript>
result = model("user").deleteByKey(1, callbacks=false);

writeOutput("Deleted user without triggering callbacks: #result#");
</cfscript>

Skips any beforeDelete or afterDelete logic.