{"__v":1,"_id":"55097a92ad1f0523008ecbd4","category":{"__v":16,"_id":"550974cc368a56170041475c","project":"55070e814bb83b2500ec9404","version":"550974cb368a561700414757","pages":["55097a674c7c3f2300aabf07","55097a8a4c7c3f2300aabf09","55097a92ad1f0523008ecbd4","55097a9faa9bd525001a065c","55097aa92dd6a11900e6e7b4","55097ab2ad1f0523008ecbd6","55097ac74c7c3f2300aabf0b","55097ace2dd6a11900e6e7b7","55097ad5dd77250d007369fa","55097adead1f0523008ecbd9","55097ae72dd6a11900e6e7b9","55097aefdd77250d007369fc","55097af8dd77250d00736a05","55097aff4c7c3f2300aabf0d","55097b07aa9bd525001a0660","55097b11dd77250d00736a07"],"reference":false,"createdAt":"2015-03-18T11:08:27.090Z","from_sync":false,"order":3,"slug":"database-interaction-through-models","title":"Database Interaction Through Models"},"project":"55070e814bb83b2500ec9404","user":"55070d24d30b3f190011b941","version":{"__v":1,"_id":"550974cb368a561700414757","forked_from":"55070e814bb83b2500ec9407","project":"55070e814bb83b2500ec9404","createdAt":"2015-03-18T12:51:23.709Z","releaseDate":"2015-03-18T12:51:23.709Z","categories":["550974cc368a561700414758","550974cc368a561700414759","550974cc368a56170041475a","550974cc368a56170041475b","550974cc368a56170041475c","550974cc368a56170041475d","550974cc368a56170041475e"],"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.4.0","version":"1.4"},"updates":[],"createdAt":"2015-03-18T13:16:02.830Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"order":4,"body":"Deleting records in Wheels is simple. If you have fetched an object, you can just call its [delete()](doc:delete) method. If you don't have any callbacks specified for the class, all that will happen is that the record will be deleted from the table and `true` will be returned.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Delete Callbacks\"\n}\n[/block]\nIf you have callbacks however, this is what happens:\n\nFirst, all methods registered to be run before a delete happens (these are registered using a [beforeDelete()](doc:beforedelete) call from the `init` function) will be executed, if any exist.\n\nIf these return `true`, Wheels will proceed and delete the record from the table. If `false` is returned from the callback code, processing will return to your code without the record being deleted. (`false` is returned to you in this case.)\n\nIf the record was deleted, the [afterDelete()](doc:afterdelete) callback code is executed, and whatever that code returns will be returned to you. (You should make all your callbacks return `true` or `false`.)\n\nIf you're unfamiliar with the concept of callbacks, you can read about them in the [Object Callbacks](doc:object-callbacks) chapter.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Example of Deleting a Record\"\n}\n[/block]\nHere's a simple example of fetching a record from the database and then deleting it.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<cfset aPost = model(\\\"post\\\").findByKey(33)>\\n<cfset aPost.delete()>\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nThere are also 3 class-level delete methods available: [deleteByKey()](doc:deletebykey), [deleteOne()](doc:deleteone), and [deleteAll()](doc:deleteall). These work similarly to the class level methods for updating, which you can read more about in [Updating Records](doc:updating-records).","excerpt":"Deleting records from your database tables.","slug":"deleting-records","type":"basic","title":"Deleting Records"}

Deleting Records

Deleting records from your database tables.

Deleting records in Wheels is simple. If you have fetched an object, you can just call its [delete()](doc:delete) method. If you don't have any callbacks specified for the class, all that will happen is that the record will be deleted from the table and `true` will be returned. [block:api-header] { "type": "basic", "title": "Delete Callbacks" } [/block] If you have callbacks however, this is what happens: First, all methods registered to be run before a delete happens (these are registered using a [beforeDelete()](doc:beforedelete) call from the `init` function) will be executed, if any exist. If these return `true`, Wheels will proceed and delete the record from the table. If `false` is returned from the callback code, processing will return to your code without the record being deleted. (`false` is returned to you in this case.) If the record was deleted, the [afterDelete()](doc:afterdelete) callback code is executed, and whatever that code returns will be returned to you. (You should make all your callbacks return `true` or `false`.) If you're unfamiliar with the concept of callbacks, you can read about them in the [Object Callbacks](doc:object-callbacks) chapter. [block:api-header] { "type": "basic", "title": "Example of Deleting a Record" } [/block] Here's a simple example of fetching a record from the database and then deleting it. [block:code] { "codes": [ { "code": "<cfset aPost = model(\"post\").findByKey(33)>\n<cfset aPost.delete()>", "language": "text" } ] } [/block] There are also 3 class-level delete methods available: [deleteByKey()](doc:deletebykey), [deleteOne()](doc:deleteone), and [deleteAll()](doc:deleteall). These work similarly to the class level methods for updating, which you can read more about in [Updating Records](doc:updating-records).