How to overwrite a file?

I am trying to overwrite the same file by saving a jpg, but it doesn’t seem to work even though file is opened with overwrite: true set.

Having a Folder, I create a file with overwrite: true and save my image as jpg into that file.
Then change the quality of jpg compression and save again, but the file doesn’t update anymore.

I have also tried creating a new File instance once again with the same name, but it also doesn’t update the file.

Any ideas?

Can you share a code snippet?

@kerrishotts I’ll try. My code at the moment is much more elaborate than the snippet.
This works first time, but if I call export function again with different quality data is never written to the file.

  1. Ask user to select a folder, this gives folderEntry with access.
const getFile = async (filename, folderEntry) => {
  return await folderEntry.createFile(`${filename}.jpg`, {
        overwrite: true,

const export = async (quality, token, documentId) => {
  await batchPlay([
          _obj: "save",
          as: {
            _obj: "JPEG",
            extendedQuality: quality,
            matteColor: {
              _enum: "matteColor",
              _value: "none",
          in: {
            _path: token,
            _kind: "local",
          documentID: documentId,
          lowerCase: true,
          saveStage: {
            _enum: "saveStageType",
            _value: "saveBegin",

const file = await getFile("Test", folderEntry)
const token = await fs.createSessionToken(file)

await export(token, 12, app.activeDocument._id)

I have also tried re-creating the file in the folder with overwrite: true and re-creating session token before trying to export again – same thing, data is not updated.
If after export I call await file.getMetadata() I can see that bytes didn’t change even though with lower quality there should be less data written into the file

I have also tried deleting the file. Here it gets even more funny.

I have a permission to folderEntry where I have created file.
If I call await file.delete() file gets deleted, but I can see an error that FileSystemProvider: You don't have permission on this file 'Test_14.jpg'.
If right after this I try to create the file again it seems to get created and I see a new file descriptor, I create a new token and call my export function. But export descriptor fails with error Error: no such file or directory

I have also tried using temporary folder and create a file there. Same problem, writing to the file again or even re-creating the file to overwrite doesn’t update file contents anymore

I have also tried creating a file in temporary folder and then deleting it but I get the same exception FileSystemProvider: You don't have permission on this file

It looks like one of the main problems is that awaiting for the save descriptor doesn’t guarantee that saving actually completed writing data into filesystem.
So calling file.getMetadata() right after saving results in No such file or directly because even though Entry was created, file was not yet saved properly so there is no file yet.