How to delete a draft with Java API properly?

We have noticed that deleting a draft doesn’t necessarily prevent Confluence from merging “unpublished content” with the new content changed over the REST API.

Steps to reproduce

  1. Create a page with content “published content” and save it.
  2. Edit this page and add “unpublished content” to it and close it without saving.
    The page shows “unpublished changes” lozenge.
  3. Update the content of this page over REST API {confluence_base_url}/rest/api/content/{contentId} and something like
{"id":"2228491","type":"page",
"title":"new page","space":{"key":"TEST"},"body":{"storage":{"value":
"<p>This is the updated text for the new page</p>","representation":"storage"}},
"version":{"number":14}}

What happens?

  • the page is updated with the new content and title
  • “unpublished changes” lozenge is gone
  • But if I open the editor of that page, the updated and unpublished content has been merged.
This is the updated text for the new page
Unpublished content

I thought it can be prevented by deleting the shared draft and I did it with:

Page draft = (Page) draftsTransitionHelper.getDraftForPage(page);
draft.remove(pageManager);

The draft was found and deleted from the DB. But the behavior was still the same. Merge content was presented in the editor. And here are my questions:

Is it a bug or intended behavior?
Where does the merged content is coming from?
Is it synchrony pushing the content into the editor?
How can I delete the draft so that the content is overridden over the REST API?

Thanks in advance