Issue
Deleting a large volume of System.FileDocument records through a microflow is slow, and the deletion rate does not keep up with the rate at which new files are created. Deleting the records with Structured Query Language (SQL) directly in the database, and the files directly in file storage, is not a supported alternative.
Environment
Applications hosted in Mendix Cloud
Cause
The System.FileDocument entity stores only the metadata of a file. The file content is kept in the configured file storage, and the Mendix Runtime maintains the link between the database record and the stored file. When an object is deleted through the Mendix Runtime, the Runtime removes the database record and, according to the com.mendix.storage.PerformDeleteFromStorage setting, the stored file.
Changes made outside the Mendix Runtime bypass this logic. Deleting records with SQL leaves the stored files without a database record (orphaned files), and deleting files from storage leaves database records that point to missing files. The Mendix Runtime does not detect such changes, and other tables or entities can still reference the deleted records. Direct changes to the database or file storage are therefore not supported.
The long duration of a microflow deletion is caused by the additional work performed for each object, such as event handlers, cascading deletes, and association lookups, not by the deletion of the record itself.
Solution / Workaround
Direct deletion of System.FileDocument records from the database or of files from file storage is not supported. Delete the records through the Mendix Runtime and reduce the time spent per object. The following steps describe the approach:
Check for event handlers on
System.FileDocumentand its specializations. Before-delete and after-delete event handlers run for every deleted object and can dominate the deletion time.Check the delete behavior of the associations that reference the entity. Cascading deletes and unindexed associations increase the time per object. Add indexes to the associations that are used during deletion.
Retrieve the objects in batches with a fixed limit, delete the batch with a single Delete activity, and end the transaction before the next batch. Compare the time per object, not the time per batch, when testing different batch sizes.
Run the deletion in parallel, for example with the Task Queue. Assign each task a separate set of objects, for example a range of creation dates, and monitor the database load.
Add a retention step to the flows that create the files, for example, a scheduled event that deletes files older than a defined age. This stops the growth of the backlog.
Verify the
com.mendix.storage.PerformDeleteFromStoragecustom runtime setting. The valueAllFilesremoves the stored file when the record is deleted. The valuesTemporaryFilesandNoFileskeep the stored file.
If the volume remains too large after these changes, create a Mendix Support ticket with the number of records, the entity names and the measured deletion time per batch.
Internal information related
293839
Additional information
Mendix documentation:
0 Comments