Contentful logo

Contentful Community

Querying for deleted entries

Hi all,

I’m trying to get batchSize of entries that have been updated since some time from. So far, the following works for most of my cases:

  public List<CMAEntry> fetchBatch(ZonedDateTime from, Integer batchSize) {

    Map<String, String> query = new HashMap<>();

    query.put("sys.updatedAt[gt]",from.toLocalDateTime().toString());
    query.put("order","sys.updatedAt");
    query.put("limit", batchSize.toString());
    
    return client.entries().fetchAll(query).getItems();
  }

However, I’d also like this to include recently deleted entries. In my head, ‘deleting’ an entry is equivalent to ‘updating’ it to no longer exist. I’d love to know if there is a way I can modify this query so that recently deleted items are also included.

Does anyone have an idea? Does this make sense? I can expand more if required.

Thanks.

Hi @sam.warner,

A possibility here would be to instead use the Sync API to fetch content that was recently deleted in your space.

You can find more information about this in here:

https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/synchronization

Here’s an implementation in JavaScript:

https://www.contentful.com/developers/docs/javascript/tutorials/using-the-sync-api-with-js/

@gabriel
Was very hopeful the SyncApi would work for us to be able to get info about deleted entries, since webhooks only give us “contentType” and “EntryId” along with the method/event Eg. DeletedEntry.

However, It appears at first glance that you need to “initialize” the api, in order to get back a token with which to use later to find out the “changes since that token’s generation”.
Not exactly ideal for serverless lambda related functions.
I guess you could fire the call manually once to get back the token and then store that… however I think then your number of returned items will grow over time. years later likely running very slow to return effectively everything.

As far as DeletedEntry , not only does it report incorrectly an unpublish as a delete but it also offers even less info about the deleted entry as it does not provide even the contentType and only tells you the id .