Skip to content

Preparing a Database for a Purge ​

Most of the work of a purge happens before anybody presses Run. The engine is strict: it refuses to start until the range it is about to delete is a settled accounting fact, the database can carry the load, and every balance it has to restate can be restated exactly. This page lists what to prepare, in the order that works best at a customer. The pre-flight check verifies almost all of it, so you can run the check early and use its refusals as your to-do list.

1. Agree the scope with the customer ​

Before touching the system, agree on four things in writing:

  1. The cut-off year. The last fiscal year that will be purged. Every year up to and including it goes in one run, or you can purge one year at a time (see One run or year by year).
  2. What happens to each document type. Which types are archived and deleted, which are only deleted, and which stay as read-only documents marked as purged.
  3. Whether there is an archive. Without an archive database the deleted rows are gone for good; with it, they can still be queried by SQL.
  4. The time window. A large purge runs for many hours (a database with about seven million documents in range took about 18 hours). Plan it for a weekend or a holiday.

2. Backups ​

Take a full backup of the main database, and of the archive database if there is one, after the last change you make during the preparation. The Run dialog asks you to confirm this in so many words: I took a backup of the main and the archive databases after the last change — «أخذت نسخة احتياطية من قاعدة البيانات الرئيسية وقاعدة الأرشيف بعد آخر تعديل». A purge can only be undone by restoring that backup.

3. The archive database ​

Skip this step if the configuration copies nothing to the archive (every copy option off and no document type set to copy to the archive). The archive checks are then skipped.

The archive is a separate, empty database that must live on the same SQL Server instance as the main database. Each chunk of work copies rows into it and deletes them from the main database in one transaction, so a failure rolls back both sides together; that only works on one instance.

  1. Create an empty database on the main database's SQL Server instance (any name; for example the main database's name followed by _Purge). The SQL login that the ERP uses for the main database must be able to read and write it.
  2. Add a second JNDI data source to the application server, identical to the main one except for its name and its database. On Tomcat that is a second Resource next to jdbc/nama in conf/context.xml, for example named jdbc/namapurge.
  3. In nama.properties add both lines:
    properties
    enable-purge=true
    purgedbsource=jdbc/namapurge
  4. Restart the server. At startup it creates the archive tables, one per archived table, named O followed by the table name (OSalesInvoice, OLedgerTrans, OLedgerTransLine, …).

Without these two lines the archive goes nowhere

If purgedbsource is missing or points at the main data source, the archive tables would be created inside the main database itself. The pre-flight check refuses that. If you set the properties without restarting, the check notices that the archive connection still points at the main database and asks for a restart.

The archive of a later purge ​

The archive database keeps its own list of the purge files that copied into it, in the table PurgeArchiveRun. Every purge reads that list before it starts, because a later purge deletes the roll-up balances of the purge before it and rolls them up again:

  • Keep using the same archive. It already holds the detail behind the old roll-up balances, so they are deleted without being copied, and a total over the archive counts every period once.
  • A new empty archive is accepted, for example when the old archive was lost. The old roll-up balances are then copied to it, because they are the only trace left of the earlier periods.
  • Any other archive is refused: one that holds a purge file which is not a finished purge file of this database (the archive of another database or of a test copy), and one that holds an older purge of this database but not the last one.

Each copy option is judged on its own. If the earlier purges copied the accounting records but not the cost records, a later purge that copies both leaves out the accounting roll-up and copies the cost roll-up.

Restore the two databases together

When you restore the main database to a backup taken before a purge, restore the archive database to the backup of the same moment, or start a new empty archive. Otherwise the archive holds a purge that the main database does not have as finished, and only running that same purge file again is accepted.

4. Replication off ​

A purge can not run while replication is enabled. Switch replication off, purge the head office database, then give each site a copy of the purged database.

5. One calendar for every company ​

Because a purge covers every company, every company's ledger must use the same calendar as the fiscal year that holds the cut-off. A company on a different calendar has neither the purge period nor period boundaries aligned to the cut-off, so its roll-ups would be posted wrongly; the configuration refuses to save.

6. The purge period ​

On the Fiscal Year screen of the last year to purge, add one more line to the periods grid:

  • Period Type: Purge Period.
  • Start Date and End Date: both equal to the end date of the year.
  • It must be the last line of the year. A year can have only one purge period.

Save the year; the period is created as a fiscal period like the others, and it is the one you choose in Up To Period on the configuration. Its end date must be before today.

If the fiscal year is revised, it can not be changed until it is unrevised: use Unrevise on the year, once per revision level (a year revised at two levels needs two unrevise steps). The customer may revise it again after the purge period is added. See Revise and Unrevise.

The purge period may be open or closed. Nothing but the roll-up documents can be posted into it (Can not use purge periods in normal documents), and the roll-ups are allowed into it even after a closing entry with Make All Fiscal Periods Closed has closed it.

7. Close the purged range ​

Two accounting conditions protect the roll-up from freezing figures that could still change:

  1. Every fiscal period that ends on or before the cut-off must be closed (Period Status = Closed), except the purge period itself. An open period could still receive postings after its balance was rolled up.
  2. Every company that has balances in the range needs a committed Closing Entry dated on or after the cut-off. Without it the roll-up would carry income-statement balances into the new year. A company that never posted anything in the range is not asked for one.

Closing entries are made the usual way — see Year-End Closing & Period Control. When the closing periods are already closed, the user who creates the closing entry may need an Ignore Closed Periods In exception on the fiscal year (Fiscal Period Control).

A closing entry can expose an old problem

At one customer a company had never been closed after a certain year, because a foreign-currency account held a value balance with no local balance and the closing entry refused it. The purge can not go past that year until the accountant corrects the account and the closing entry commits. Discover this early: create the closing entries during the preparation, not on the night of the purge.

8. Books and terms for the roll-up documents ​

The roll-up documents need their own books and terms. Create them before the configuration:

ForBookTerm
Purge JournalA document book for the type Purge Journal, marked SystemA document term for the type Purge Journal, marked System. Required when there is any balance to roll up.
Purge Stock ReceiptA document book for the type Purge Stock Receipt, marked SystemA document term for the type Purge Stock Receipt, marked System. Always required.

On the Purge Journal term, set Purge Roll-up Clearing Account. It is required as soon as one company's roll-up needs more than 1,000 lines (which is the normal case on a real database): every journal of a split roll-up is balanced by one line on this account. Use a balance-sheet account dedicated to the purge, so that it is easy to see that it ends at zero.

Some customers require a master group on every book and term; give the purge books and terms one too.

9. Indexes and old foreign keys ​

The purge deletes in chunks, and each chunk relies on an index to find its rows; one missing index on a table of fifty million rows turns hours into days. The pre-flight check lists every index the purge needs and refuses to start while one is missing.

Use Create Missing Indexes (on the configuration screen, or Create missing indexes on the Purge Monitor). It:

  • creates every missing index the purge needs, each in its own step, so a stopped run keeps the ones already built;
  • drops the old foreign keys to approval cases and attachments that databases created by versions before 2020 still carry. The current data model does not have them, and with them every deleted approval case or attachment makes SQL Server search hundreds of tables (a test showed 5 to 16 seconds per approval case).

It changes the database schema, so it needs the admin user signed in through the Namasoft CRM. Each index build locks its table while it runs: run it in a quiet hour. On a database of about 400 GB it created 62 indexes in under two minutes, and dropping several hundred old foreign keys took a few minutes.

10. Drain the queues ​

The purge refuses to start while work dated in the range is still waiting:

  • Ledger and inventory requests dated on or before the cut-off that are not processed (initial, retry, processing or failed). Process or resolve them from Business Requests.
  • Saved business requests still waiting to be picked up.
  • Stock ages queues. They normally drain within seconds.

Right after you commit closing entries, their requests need a few minutes to post. Wait, then check again.

11. Leave the purge properties alone ​

Three server properties set the chunk sizes. They are not needed in normal use; if one is set, it must be inside its range or the purge refuses to start.

PropertyDefaultAllowed
purge.trans.batch.rows — transaction headers per chunk10000100 – 100000
purge.doc.batch.rows — documents per chunk2000100 – 20000
purge.iterations.per.call — chunks per step before the next batch gets a turn201 – 1000

12. Who can do it ​

Saving a purge configuration and running it are protected twice: the user must be the admin user, and the operation belongs to a critical security level that the Namasoft CRM has to allow for the signed-in user. Otherwise the user sees You are not authorized to change the field {0} because it belongs to the security level {1}, please contact technical support team — «أنت لا تمتلك صلاحية التعديل على الحقل {0} لأنه ينتمي لمستوى الأمان {1} برجاء التواصل مع الدعم الفني». In practice a purge is run by Namasoft support or an implementer signed in through the CRM.

Messages you may see ​

MessageWhyWhat to do
Purge period must be the last periodThe purge period is not the last line of the fiscal year. No Arabic text exists; it shows in English on Arabic screens too.Move it to the end.
Purge period end date and start date must be the same as the year end dateThe purge period has other dates. English only.Set both dates to the year's end date.
A year can have only one openning period, one closing period, and one purge periodA second purge period was added. English only.Keep one.
Purge can not start: {0} fiscal period(s) ending on or before {1} are still open ({2}); close every period inside the purged range first - the roll-up freezes balances an open period could still change — «لا يمكن بدء الحذف: {0} من الفترات المالية المنتهية في {1} أو قبله ما زالت مفتوحة ({2}). أغلق كل الفترات داخل مدى الحذف أولا، لأن التجميع يثبت أرصدة قد تتغير في فترة مفتوحة»Periods inside the range are open.Close them.
Purge can not start: the legal entity {0} has no committed {1} dated on or after {2}; close the purged range into retained earnings first, otherwise the roll-up carries income-statement balances forward — «لا يمكن بدء الحذف: الشركة {0} ليس لديها {1} معتمد بتاريخ {2} أو بعده. أقفل مدى الحذف في الأرباح المحتجزة أولا، وإلا سيرحل التجميع أرصدة قائمة الدخل»The company has no closing entry at or after the cut-off.Create and commit the closing entry.
Purge can not start: the ledger roll-up of the legal entity {0} needs {1} lines, more than the {2} lines of one journal, and the term {3} has no Purge Roll-up Clearing Account; set the account on the term first - every journal of a roll-up that is split over several journals is balanced by one line on that account — «لا يمكن بدء الحذف: قيود الأرصدة المجمعة للشركة {0} تحتاج إلى {1} سطر، وهو أكثر من {2} سطر يحملها القيد الواحد، والتوجيه {3} ليس به حساب تسوية قيود الأرصدة المجمعة لحذف البيانات. حدد الحساب في التوجيه أولا، لأن كل قيد من قيود الأرصدة المجمعة الموزعة على أكثر من قيد يوازن بسطر واحد على هذا الحساب»The Purge Journal term has no clearing account.Set Purge Roll-up Clearing Account on the term.
The legal entity {0} calendar is {1} and you are trying to purge upto the period {2} and this period calendar is {3} — «تقويم الشركة {0} هو {1} وأنت تحاول الحذف حتى الفترة {2} وتقويم هذه الفترة هو {3}»A company uses another calendar.A purge is not possible until every company uses one calendar.
Purge can not be used with replication, please disable replication and do the purge, then distribute a copy of the after-purge database — «لا يمكن استخدام الحذف مع الريبليكشن، يرجى تعطيل الريبليكشن وتنفيذ الحذف ثم توزيع نسخة من قاعدة البيانات بعد الحذف»Replication is enabled.Switch it off for the purge.
Purge can not start: the config {0} copies purged data to the archive database, but the {1} property is not set (or points at the main datasource jdbc/nama), so the archive would be written into the main database itself; set {1} to a separate archive datasource first — «لا يمكن بدء الحذف: الملف {0} ينسخ البيانات المحذوفة إلى قاعدة البيانات الأرشيفية، لكن الخاصية {1} غير مضبوطة (أو تشير إلى مصدر البيانات الرئيسي jdbc/nama)، وبالتالي سيكتب الأرشيف داخل قاعدة البيانات الرئيسية نفسها. اضبط {1} على مصدر بيانات أرشيفي منفصل أولا»purgedbsource is missing.Step 3.
Purge can not start: the archive datasource connection points at the main database itself ({0} on {1}); the {2} property was probably changed after server boot - restart the server so the archive datasource binding is re-created — «لا يمكن بدء الحذف: اتصال مصدر البيانات الأرشيفي يشير إلى قاعدة البيانات الرئيسية نفسها ({0} على {1})، وغالبا تم تغيير الخاصية {2} بعد تشغيل الخادم - أعد تشغيل الخادم ليعاد ربط مصدر البيانات الأرشيفي»The property was set without a restart.Restart the server.
Purge can not start: the archive database {0} is on the instance {1} while the main database is on {2}; archive inserts run on the main connection in the same transaction as the deletes, so the archive database must live on the main instance - point {3} at a database of {2} — «لا يمكن بدء الحذف: قاعدة البيانات الأرشيفية {0} موجودة على الخادم {1} بينما قاعدة البيانات الرئيسية على {2}. عمليات الإدراج في الأرشيف تنفذ على الاتصال الرئيسي في نفس معاملة الحذف، لذا يجب أن تكون قاعدة البيانات الأرشيفية على الخادم الرئيسي - اجعل {3} يشير إلى قاعدة بيانات على {2}»The archive is on another SQL Server instance.Create it on the main instance.
Purge can not start: the archive database {0} does not exist or is not visible to the main database connection; archive inserts are executed on the main connection as [{0}].dbo.[O...] — «لا يمكن بدء الحذف: قاعدة البيانات الأرشيفية {0} غير موجودة أو غير مرئية لاتصال قاعدة البيانات الرئيسية، حيث تنفذ عمليات الإدراج في الأرشيف على الاتصال الرئيسي بالصيغة [{0}].dbo.[O...]»The main database's login can not see the archive.Give that login access to the archive database.
Purge can not start: the archive database {0} is missing {1} archive table(s): {2}; boot a server bound to that database so its schema is created, then re-run the purge — «لا يمكن بدء الحذف: قاعدة البيانات الأرشيفية {0} ينقصها {1} من جداول الأرشيف: {2}. شغل خادما مرتبطا بقاعدة البيانات هذه ليتم إنشاء جداولها، ثم أعد تشغيل الحذف»The archive tables were not created.Check enable-purge=true, restart, check again.
Purge can not start: the archive database {0} holds the data of purge file {1} (cut-off {2}), which is not a finished purge file of this database; it is the archive of another database, or this database was restored to an older state. Point {3} at the archive database of this database or at a new empty one — «لا يمكن بدء الحذف: قاعدة البيانات الأرشيفية {0} تحتوي على بيانات ملف الحذف {1} (تاريخ نهاية الحذف {2}) وهو ليس ملف حذف منتهيا في قاعدة البيانات هذه؛ فهي أرشيف قاعدة بيانات أخرى، أو تمت استعادة قاعدة البيانات هذه إلى حالة أقدم. وجّه {3} إلى قاعدة البيانات الأرشيفية الخاصة بقاعدة البيانات هذه أو إلى قاعدة بيانات جديدة فارغة»The archive belongs to another database or to a test copy, or the main database was restored without its archive.Point purgedbsource at the right archive or at a new empty database, restart, check again. See The archive of a later purge.
Purge can not start: the archive database {0} holds the rows of the option {1} of purge file {2} but not of the last purge file {3}; the roll-up of {3} restates them, so copying it would count them twice. Point {4} at the archive database that {3} used or at a new empty one, or clear the option {1} — «لا يمكن بدء الحذف: قاعدة البيانات الأرشيفية {0} تحتوي على سجلات الاختيار {1} لملف الحذف {2} وليس لآخر ملف حذف {3}؛ وأرصدة الترحيل الخاصة بالملف {3} تتضمن هذه السجلات، فنسخها يجعلها محسوبة مرتين. وجّه {4} إلى قاعدة البيانات الأرشيفية التي استخدمها الملف {3} أو إلى قاعدة بيانات جديدة فارغة، أو ألغِ الاختيار {1}»The archive is an old one: the last purge copied this kind of records to another archive, or did not copy them at all.Use the archive of the last purge or a new empty one; or clear that copy option on the configuration, and its records are then deleted without a copy.
Purge can not start: the property {0} is set to {1}, which is outside the allowed range {2} - {3}; a batch size outside that range would let purge steps mark themselves complete without deleting anything - correct the property first — «لا يمكن بدء الحذف: الخاصية {0} قيمتها {1}، وهي خارج النطاق المسموح {2} - {3}. حجم دفعة خارج هذا النطاق قد يجعل خطوات الحذف تعتبر نفسها مكتملة دون حذف أي شيء - صحح الخاصية أولا»A chunk-size property is out of range.Step 11.
Purge can not start: {0} dated on or before {1} are not fully processed yet. Process or resolve them from the Business Requests view first — «لا يمكن بدء الحذف: {0} بتاريخ {1} أو قبله لم تتم معالجتها بالكامل بعد. عالجها أو قم بحلها من شاشة طلبات الأعمال المحفوظة أولا»Requests in the range are waiting or failed.Step 10.
Creating the purge indexes changes the database schema and needs an admin user logged in through the Namasoft CRM — «إنشاء فهارس الحذف يغير هيكل قاعدة البيانات ويتطلب مستخدما مسؤولا دخل عن طريق نظام CRM الخاص بنماسوفت»Create Missing Indexes was used without a CRM sign-in.Sign in as admin through the Namasoft CRM.