{"title":"Insert, Modify, ModifyAll, Delete, DeleteAll, and Truncate methods","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/devenv-insert-modify-modifyall-delete-and-deleteall-methods/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-insert-modify-modifyall-delete-and-deleteall-methods, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-insert-modify-modifyall-delete-and-deleteall-methods","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"Describes how to use the Insert, Modify, ModifyAll, Delete, and DeleteAll methods in Business Central","date":"2025-09-30","meta":{"topic":"article"},"outline":[{"level":1,"title":"Insert, Modify, ModifyAll, Delete, DeleteAll, and Truncate methods","slug":"insert-modify-modifyall-delete-deleteall-and-truncate-methods","at":0,"chars":9189},{"level":2,"title":"Insert method","slug":"insert-method","at":822,"chars":1047},{"level":2,"title":"Modify method","slug":"modify-method","at":1869,"chars":581},{"level":2,"title":"ModifyAll method","slug":"modifyall-method","at":2450,"chars":826},{"level":2,"title":"Delete method","slug":"delete-method","at":3276,"chars":1758},{"level":2,"title":"DeleteAll method","slug":"deleteall-method","at":5034,"chars":1115},{"level":2,"title":"Truncate method","slug":"truncate-method","at":6149,"chars":1262},{"level":2,"title":"When to use Truncate or DeleteAll","slug":"when-to-use-truncate-or-deleteall","at":7411,"chars":1294},{"level":2,"title":"Related information","slug":"related-information","at":8705,"chars":484}],"text":"# Insert, Modify, ModifyAll, Delete, DeleteAll, and Truncate methods\nThe following methods maintain the database by adding, modifying, and removing records:\n methods-auto/record/record-insert--method\n methods-auto/record/record-modify-method\n methods-auto/record/record-modifyall-method\n methods-auto/record/record-delete-method\n methods-auto/record/record-deleteall-method\n methods-auto/record/record-truncate-method\nThese methods are some of the most frequently used AL methods.\nSome of these methods return an optional Boolean value that indicates whether the method succeeded. If you don't handle the return value in your code, a runtime error occurs when a method returns false. If you handle the return value by testing its value in an if statement, no error occurs, and you must take corrective action in the code.\n## Insert method\nThe Insert method inserts a record in a table. Insert has the following syntax.\n```al\n[Ok := ]  Record.Insert([RunTrigger: Boolean[, InsertWithSystemId: Boolean]])\n```\nA record must be assigned a SystemId. You can assign your own value or have the platform assign an autogenerated value. The following example inserts a new record, with the SystemId, No., and Name fields specified in the assigned values, while other fields have their default values. If the No. field is the primary key of the Customer table, then the record is inserted in the Customer table unless the table already contains a record with the same primary key. In this case, you receive an error message because the return value isn't tested.\n```al\nvar\n    Customer: Record Customer;\nbegin\n    Customer.Init;\n    Customer.SystemId := '{B6666666-F5A2-E911-8180-001DD8B7338E}';\n    Customer.\"No.\" := '4711';\n    Customer.Name := 'Andrew Dixon';\n    Customer.Insert(false, true);\nend;\n```\n> IMPORTANT\n> After the SystemId is set on a record, it can't be changed.\n## Modify method\nModify modifies a record that already exists. See methods-auto/record/record-modify-method. Modify has the following syntax:\n```al\n[Ok :=] Record.Modify([RunTrigger])\n```\nModify returns an optional Boolean value. It returns true if the record to be modified exists; otherwise, it returns false.\nThe following example changes the name of customer 4711 to `Richard Roe`. This example requires that you create the following variable.\nVariable|Data type|Subtype\nCustomer|Record|Customer\n```al\nCustomer.Get('4711');\nCustomer.Name := 'Richard Roe';\nCustomer.Modify;\n```\n## ModifyAll method\nModifyAll performs a bulk update of records. See methods-auto/record/record-modifyall-method. ModifyAll has the following syntax:\n```al\nRecord.ModifyAll(Field, NewValue [, RunTrigger])\n```\nModifyAll uses the current filters. This means that you can perform the update on a specified set of records in a table. ModifyAll returns no value, nor does it cause an error if the set of records to be changed is empty.\nIn the following example, the SetRange statement selects the records where `Salesperson Code` is PS. The ModifyAll statement changes the Salesperson Code of these records to JR. The example requires that you create the following variable.\nVariable|Data type|Subtype\nCustomer|Record|Customer\n```al\nCustomer.SetRange(\"Salesperson Code\",'PS','PS');\nCustomer.ModifyAll(\"Salesperson Code\",'JR');\n```\n## Delete method\nDelete deletes a record from the database. See methods-auto/record/record-delete-method. Delete has the following syntax.\n```al\n[Ok :=] Record.Delete([RunTrigger])\n```\nThe record that you want to delete must be specified by using the values in the primary key fields before you call this method. Delete does take filters into consideration.\nThe following example shows how to use Delete to delete the record for customer number 4711. The following example requires that you create the following variable.\nVariable|Data type|Subtype\nCustomer|Record|Customer\n```al\nCustomer.\"No.\" := '4711';\nCustomer.Delete;\n```\nDelete returns an optional Boolean value. It returns true if the record could be found; otherwise, it returns false. Unless you test this value in your code, a run-time error occurs when Delete fails.\nWhen you're developing your own applications, you should consider the following scenario:\n1. Retrieve a record from the database.\n2. Perform various checks to determine whether the record should be deleted.\n3. Delete the record if step 2 indicated that you should.\nThis scenario can cause problems in a multi-user environment. Another user can modify or delete the same record between your performing steps 2 and 3. If the record is modified, then perhaps the new contents of the record could change your decision to delete it. If another user deletes it, you can get a run-time error if you verified that the record existed (in step 1). If the design of your application indicates that you can encounter this problem, you should consider using the LockTable method. LockTable should be used sparingly because this method degrades performance. Learn more about the LockTable method in methods-auto/record/record-locktable-method.\n## DeleteAll method\nDeleteAll deletes all the records that are specified by the filter settings. If no filters are applied, it deletes all the records in the table. See methods-auto/record/record-deleteall-method. DeleteAll has the following syntax:\n```al\nRecord.DeleteAll([RunTrigger])\n```\nThe following example deletes all the records from the Customer table where the `Salesperson Code` is PS. This example requires that you create the following variable.\nVariable|Data type|Subtype\nCustomer|Record|Customer\n```al\nCustomer.SetRange(\"Salesperson Code\", 'PS', 'PS');\nCustomer.DeleteAll;\n```\n> NOTE\n> When you use `DeleteAll(true)`, a copy of the AL variable with its initial values is created. This means that when you use `DeleteAll(true)` to run the OnDelete trigger, all the changes that were made to the variables in the method or codeunit that's making the call, can't be seen in the OnDelete trigger. If you want to see the changes that you made to the variables, you must use `Delete(true)` in a loop. There's no difference in performance between using `DeleteAll(true)` and using `Delete(true)` in a loop.\n## Truncate method\nLike Delete, the Truncate method also deletes records from a table. However, the Truncate method provides a high-performance way to remove large volumes of rows from a table by skipping row‑by‑row deletions. Instead of iterating and deleting each row, the platform uses a bulk operation that significantly reduces execution time and logging overhead. The method offers options to either reset AutoIncrement values to 0 or preserve their previous values. It also validates delete permissions before running.\nTruncate has the following syntax:\n```al\n[Ok := ]  Record.Truncate([ResetAutoIncrement: Boolean])\n```\nTruncate returns false when truncate isn't supported<#when-to-use-truncate-or-deleteall>.\nIf you supply filters, the platform copies the rows you want to keep to a temporary table, truncates the original table, and then moves the kept rows back. This process maintains the speed benefits of bulk deletion while allowing filtered removals.\nThe following example deletes all the records from the MyTable table where the Location Code is Red. This example requires that you create the following variable.\nVariable|Data type|Subtype\n-|-|-\nMyRec|Record|MyTable\n```al\nMyRec.SetRange(\"Location Code\", 'Red');\nOk :=  MyRec.Truncate(true)\n```\n## When to use Truncate or DeleteAll\nUse Truncate when you need to clear or reduce most of a table. Example scenarios include cleanup, reset between tests, or bulk archival workflows. DeleteAll (removing every row when no filters are applied) is simpler but typically slower than Truncate for large tables, because it might still trigger per-row operations such as triggers or constraints. Use DeleteAll when not deleting a majority of the table or if the table doesn't support Truncate.\nTruncate isn't supported in the following cases:\n Temporary tables, system tables, and tables of type other than Normal.\n Running inside try functions.\n Tables that have a security filter applied.\n When the current filters contain flow fields, or use a high number of marked records.\n When there are event subscribers for the OnAfterDelete or OnBeforeDelete triggers of the table.\n Tables with media fields.\nTruncate is supported when the global DatabaseDelete trigger is implemented (which is different for normal DeleteAll in bulk), meaning that trigger doesn't get called on Truncate.\nYou can use the return value of Truncate to attempt high-performance deletion, and if it isn't supported, fall back to using DeleteAll. For example:\n```al\nIf not MyRec.Truncate(true) then\n  MyRec.DeleteAll(false);\n```\n## Related information\nRecordRef.Truncate([Boolean]) method<methods-auto/recordref/recordref-insert-boolean-method>\nRecordRef.Modify([Boolean]) method<methods-auto/recordref/recordref-modify-method>\nRecordRef.Delete([Boolean]) method<methods-auto/recordref/recordref-delete-method>\nRecordRef.DeleteAll([Boolean]) method<methods-auto/recordref/recordref-deleteall-method>\nAL methods<methods-auto/library>\nAL Language reference overview<../developer>\ndevenv-table-system-fields#systemid"}