Cleaning up Media Orphans with the Data Administration tool in Business Central

Did you ever wonder how to delete Media Orphans in Business Central?

Even more – did you know that Media Orphans are a real thing – and can consume a great deal of your database capacity?  It is a thing – and you should be aware!

Well – here’s a story that made me dive into an undocumented part of Business Central…

Database Capacity

Data Capacity is something we (customer/partners) need to take into account.  If you wonder why – well – there are limits.  Read more about it here: Managing Capacity – Business Central | Microsoft Learn

There are a few ways for you to follow up on that.  Like the “Capacity” page in the Admin Center, which on the bottom of the page, shows you also the links to the table information per environment to go more into details which tables consumes most capacity.

And in a lot of cases, you’ll see something like this:

Or in other words: you’ll see that an abundant amount of data is lost on Media.  Pictures, email attachments, .. it could virtually be any kind of value in any Media-datatype-field that you upload to the system.

If you’re not careful – these files are typically big, so they take a lot of space in the database. 

Also, these “BLOBs” are not saved in the table itself anymore, but referenced from a “Tenant Media” table, which would contain the BLOB. 

Example:
the Item-table has a picture.  This picture is saved as a BLOB in the “Tenant Media”-table, and referenced with its GUID to the Item Table.  That’s in a nutshell – there’s a tad more to it, but it comes down to having all these blobs in one or two tables (also the Thumbnail-table, as you can see in the screenshot), and that’s it.  And that’s why this table stands out in the “Table Information” page.

Media Orphans

So what happens when there are records in the Media-table, but there are NO references anymore to ANY record in ANY table of ANY extension.  In this case, the record just consumes database space, and will never ever anymore be referenced from any kind or record.  Not ever.  Ever!

That’s when we speak of “Media Orphans“.  They take space, and are completely useless.  Useless space, and if it happens, a LOT of it.  So ..

How do we remove Media Orphans?

Well – that’s another story. 

Since quite some versions, it’s easier to detect Media Orphans in code, thanks to:

The methods simply provide a list with guids, one item is one Orphan.

I even covered this in my Telemetry session on BCTechDays on how it’s possible to keep track of Media Orphans for multiple customers, which looks something like this in the dashboard:

Back in the days, I created a solution that had 2 pages (one for Media, the other for MediaSet), and some actions to gather all orphans by searching through all Media fields in all tables, to find out if a certain value of the Media-table is used or not.  If not used in any of fields, it’s an Orphan, and I allowed it to remove the Orphan.

You can find the solution here: waldo.MediaOrphans/Src/Data/Media at main · waldo1001/waldo.MediaOrphans (github.com)

I never blogged about it, because it was slow, and I didn’t trust anyone else but me with it.  So – use at your own risk 🤪!  Also know – it’s outdated!  These days, we have new tools!

When I was browsing through the default application, I found this:

A cleanup codeunit in the System Application, which they made available under “Data Administration”

This made me wonder – did I miss something?  And after quite some searching, it seems to be quite an undocumented new feature since v23.5.  I challenge you, try to use Copilot Chat or Microsoft Learn. You won’t find anything.

UNTIL I used CentralQ.ai, which made me end up on this blog: Media on temporary tables – mmilince.com.  Honestly – I was already this far in my blog when I noticed this blogpost, but I decided to post mine anyway ;-).  The more docs about this, the better, I guess ;-).

For me, this “Data Administration” tool, and the “Delete Detached Media” were completely new to me.  And also for the Data Administration tool, Microsoft lacks documentation in my opinion.  I could find these blogs as well about it:

Data Administration

When you search for “Data Administration” in the “Tell me”, you actually end up on yet another overview of the “Table Information”:

I found this very interesting, because this data is actually saved in a buffered table, which you can even schedule:

So you have a scheduled way to refresh table information (which can be time consuming).  The reason why I find this very interesting is because I actually want this information in Telemetry.  I have a proposal for a PR here: Daily telemetry: Table Information · Issue #780 · microsoft/BusinessCentralApps (github.com)

But .. we’re wandering off … 😉 .. let’s get back to the topic.

In this page, somewhat hidden, you can also find the “Delete Detached Media”:

And it turns out this function was added only very recent, in v23.5.

It will open a page, where you can “Load Detached Media” and have an overview of all your “Media Orphans”:

You can even see the media (if picture), download the media, and .. delete.

Now, for deletion, I would advice to not do it one by one, or all in the client, but to schedule a cleanup task, to run it in background:

And there you go – Orphaned Media: Solved! ✅

References

Just for you convenience, and by means of a summery – some references:

5.00 avg. rating (98% score) - 3 votes

Permanent link to this article: https://www.waldo.be/2024/07/12/cleaning-up-media-orphans-with-the-data-administration-tool-in-business-central/

1 ping

  1. […] Cleaning up Media Orphans with the Data Administration tool in Business Central […]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.