Contentful logo

Contentful Community

Is it possible to automatically delete records which are no longer live on our website?

We have a lot of records which are no longer visible on our website and are no longer required, such as:

  • Records in a draft status which are more than X weeks old
  • Archived records
  • Records in a published status but we have a custom ‘active until’ field which means they are not displayed on the website after that date
  • Media assets which are no longer attached to a content item or linked to from a content item

Is it possible to identify and automatically delete records meeting these and similar criteria?

Does anyone have any other tips about how they manage the number of records so it does not go over the plan limit?

Hi Michael,

You will need to build a custom script to identify the assets, and use Content Management API (CMA) to delete it automatically.

Records in a draft status which are more than X weeks old

Using parameter sys.createdAt[gt]=2019-01-01T00:00:00.000Z to query all asset, in the API response, if an asset is not published, it doesn’t have publishedVersion attribute, you can use it as an indicator for draft asset.

Archived records

In API response of querying asset collection, for archived asset, you can find archivedVersion attribute

Records in a published status but we have a custom ‘active until’ field which means they are not displayed on the website after that date

Query entry collection with parameter, content_type={content-type-id}&fields.{field-id-active-until}[gte]=2019-01-01T00:00:00.000Z

Media assets which are no longer attached to a content item or linked to from a content item

When using endpoint, https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/links-to-asset/query-entries/console/curl, in the API response, when “total”: 0, it means the asset isn’t linked from any entry

After identifying the assets to be deleted, you can use CMA to (unpublished and) delete assets: https://www.contentful.com/developers/docs/references/content-management-api/#/reference/assets/asset/delete-an-asset/console/curl

I hope this will help! :slight_smile:

Thank you. That is very helpful.