Delete a legal entity (company accounts) in Dynamics AX 2012

The information in this post is based on Dynamics AX 2012 R3. It may or may not be valid for other versions.

Notice: There is a very real risk of data loss if you follow the instructions in this post. In fact, it is exactly about deleting data. You should generally not delete legal entities in production environments, only in test or development environments. Usage of this information is at your own risk.

I was recently tasked with completely removing a legal entity (also know as company accounts or data area) from a Dynamics AX 2012 environment. The reason the user didn’t just do it herself from the Legal entities form was that it spewed out some ugly errors about not being able to delete records in certain tables.

After looking into it, I discovered that you cannot delete a legal entity that has transactions. That makes sense, so the obvious solution would be to remove the transactions first, and then delete the legal entity.

Fortunately, Dynamics AX provides a class for removing the transactions of a legal entity, so this is fairly straightforward – although, quite time consuming if you have a lot of data.

The procedure is:

    1. Open the AOT and find the class SysDatabaseTransDelete (in some earlier versions of AX it was just called “DatabaseTransDelete”).
    2. Make absolutely sure you’re currently in the company for which you want to delete the transactions!
    3. Run the class found in step 1. It will prompt you to confirm that you wish to remove the transactions. Again, make absolutely sure that the company it asks about is the one for which you want to delete the transactions!
    4. Let the task run. This can take quite a while if you have many transactions.
    5. Once it’s done, return to the Organization administration / Setup / Organization / Legal entities form. Make sure you’re not in the company you want to delete at this point, as you can’t delete the current company.
    6. Select the company you want to delete and press the “Delete” button (or Alt+F9).
    7. Confirm that you want to delete the company. This will also take a while, as it’s now deleting all the non-transactional data in the company.
    8. Sit back, relax and revel in the glory of a job well done! 🙂