Get an assets linked entries

Is there anyway to achieve this functionality with the API. As seen on the app.contentful platform.

Screenshot 2020-02-10 at 09.51.55

In other words does the API expose the links on a given media asset. Similar to the “There is one entry that links to this asset” section of the web app.

Regards,
Wes

The Content Delivery API has 2 search parameters for this exact purpose: links_to_entry and links_to_asset. See this link for more details.

To search for entries which have a field linking to a specific entry

I’m not looking for anything specific. I’m just trying to determine what, if any linked entries are attached to a given asset. In other words, can the "{entry_id}" be left empty to return all the entries that link to the asset. I’m trying to replicate what we see on the contentful web app.

Ultimately what I’m trying to do is remove all the assets in the Media area that are not in use (linked to an entry). The only way to do this currently, is by manually clicking each asset and seeing if the link icon is present.

Just get all your asset IDs and iterate through the list and for each make a CDA query using the links_to_asset search parameter and make note of all the responses where “total” is 0. The response will look like this:

  {
"sys"    : {
"type": "Array"
  },
"total": 0  ,
"skip": 0  ,
"limit": 100  ,
"items": []
}

That’ll produce a list of asset IDs that have no entries linking to them (aka assets that you aren’t using anywhere). Then you can iterate through this unused assets list and delete each one.

1 Like

Ahh ok. So if you have thousands of assets and thousands of entries its gonna take millions of queries? Definitely the only way of doing it? Is there a way to expose this on the front end contetnful web app. Potentially showing a column for links?

Edit: Charlie has saved the day. Thanks Charlie

1 Like

It’ll take 1 API call for each asset you have. That’s all. This is exactly how the web app is doing it as well.

Your suggestion about having a column for links in the entry/asset listing is a good idea and I’ll pass that feedback on to our product team.