Contentful logo

Contentful Community

Sync API - deleted entities not coming through with Java SDK

#1

Hello,
I have downloaded the Java boilerplate and adapted it to use the sync API to get all CRUD updates from our Content Preview Space. The Java code works for additions and updates, but I never get any deletions coming back. I have tried all SyncTypes without success.

Here is the single Java class (Using SDK Version 10.3.1):

package com.contentful.hello;

import com.contentful.java.cda.CDAClient;
import com.contentful.java.cda.CDAEntry;
import com.contentful.java.cda.SyncType;
import com.contentful.java.cda.SynchronizedSpace;

import java.net.URL;
import java.util.Map;
import java.util.Set;

public class Main {

    private static final String PREVIEW_ACCESS_TOKEN = "XXXXX";
    private static final String SPACE_ID = "YYYYYY";

    public static void main(String[] args) throws Exception {

        final CDAClient client = CDAClient
                .builder()
                .setEndpoint("https://preview.contentful.com/")
                .setToken(PREVIEW_ACCESS_TOKEN)
                .setSpace(SPACE_ID)
                .build();

        // all updates - CRUD
        SyncType allUpdates = SyncType.allEntries();
        SynchronizedSpace space = client.sync(allUpdates).fetch();

        System.out.println("Initial entries for sync type:");
        logEntries(space);


        //loop forever waiting for updates
        while (true) {
            Thread.sleep(30000);
            URL url = new URL(space.nextSyncUrl());
            String syncToken = url.getQuery().split("=")[1];
            space = client.sync(syncToken).fetch();
            logEntries(space);
        }
    }

    private static void logEntries(SynchronizedSpace space) {
        Map<String, CDAEntry> entries = space.entries();
        Set<String> deletedEntries = space.deletedEntries();
        Set<String> deletedAssets = space.deletedAssets();

        if (space.items().size() == 0)
            return;

        System.out.println("------------------------");
        for (String key : entries.keySet()) {
            System.out.println(String.format("\tUpdated %s: %s", key, entries.get(key).contentType()));
        }
        for (String key : deletedEntries) {
            System.out.println(String.format("\tDeleted Entry - %s", key));
        }
        for (String key : deletedAssets) {
            System.out.println(String.format("\tDeleted Asset - %s", key));
        }

    }
}

Any suggestions?

Serge Merzliakov
Senior API Engineer
Qantas Loyalty

0 Likes

#2

Hello,

thank you for your question. I can see two issues:

a) Using sync is not supported in preview. https://www.contentful.com/developers/docs/references/content-preview-api/#/introduction/using-the-preview-api

  • the only thing that is supported, is initial sync, on preview.

b) I see you using setEndpoint but if you are using preview, you should be using .preview().

I hope that helps,
Mario

PS: Please don’t post on threads that are just barely related to your problem: Not getting Deleted Entry?

0 Likes

#3

Thanks for letting me know that Preview Does Not Support Sync. I could have sworn it worked for additions and updates. Perhaps I missed something.

1 Like

#4

Can we Webhooks as a substitute to the Sync API, or do they use the same codebase (and therefore the answer is no…)

Serge

0 Likes

#5

Hey Serge,

If you use an environment instead of preview, you could use sync with diffs and the deleted entries. Sadly merging back the content from a non master environment would be a bit trickier then using preview.

To answer your question directly: You could setup a webhook to get fired whenever an entity gets deleted. You would then only get it’s id in the response of the webhook, but you could then purge this id from your local cache, if this is what you want to achieve.

Greetings,
Mario

0 Likes