Automated Detached Media Cleanup

Introduction

In Business Central, there are two ways to store files.

Either as Blobs or as Media types.

In Media types, we store the files in the database (Tenant Media ID - 2000000184) and then we refer this record in other tables.

Whenever there are no records referring a Media, it is deleted from the database.

Media types are much more performant as they support caching whereas BLOBs needs to be fetched from the SQL Server every time they are used.

Microsoft has moved most of the fields from using Blobs to Media types.

However, in scenarios with a high load of writes and deletes, it is possible that there may end up orphaned media records i.e. Media records that are not referenced anywhere.

In order to tackle such cases, Microsoft has recently announced the FindOrphans procedure which would return a list of GUIDs of such orphaned Media Types which could then we dealt with as needed.

There’s also another tool that has been provided by Microsoft which uses this newly added procedures and helps us in reduce storage usage needlessly, which is what are going to discuss today.

Pre-requisites

Business Central Cloud/On Prem

References

Media Data Type - Microsoft Learn
Media.FindOrphans() Method - Microsoft Learn

Configuration

You can access the “Detached Media Cleanup” from the “Data Administration” tool in Business Central.



Under “Data Cleanup -> Miscellaneous”.



Here, we have the actions to Load any detached Media and then delete them individually. I tried many ways to recreating the scenario where detached media exist but I couldn’t get it to work, so if any of you know how to re-create this do let me know!



Finally there’s also an option to run it automatically periodically to prevent buildup of detached media taking up storage space.


Conclusion

Thus we saw how we can use the inbuild tool “Detached Media Clean up” to free up database space.

Happy Coding!

Comments

Popular posts from this blog

Using Notifications in Business Central via AL

Actionable Error Messages in Business Central

Configure Gmail SMTP service in Business Central without "Allow Less Secure" setting