title: Overwrite an Image File with Google Apps Script
body: <p>Can I overwrite an image file with Google Apps Script?  I've tried:</p>
  
  <pre><code>file.setContent(newBlobImage);
  file.replace(newBlobImage);
  </code></pre>
  
  <p>Neither of those work.  <code>.setContent()</code> will delete whatever data was in the file, and it looks like maybe it just writes the variable name as text, or something like that.  I'm assuming that both <code>.setContent()</code> and <code>.replace()</code> are meant for text documents, and maybe that's why they don't work.</p>
  
  <p>If it were a text file, or a spreadsheet, I might be able to clear it, then append new content.</p>
  
  <p>I can trash the file, then create a new one, but I'd rather not if there is some other way.</p>
  
  <p>If I write a file with the same name, it won't overwrite the existing file, it creates a another file with the same name.</p>
  
  <p>The only way I've been able to trash the file is with <code>DocsList</code> and the only success I've had with creating an image file is with <code>DriveApp</code>.  So I have to trash the file with <code>DocsList</code>, then create another file with <code>DriveApp</code>.</p>
  
  <p>Well, I've figured out how to delete the file without sending it to the trash, so I won't need to clean out the trash later.  The <code>Google Drive SDK</code> inside of Apps Script has a <code>remove</code> method that didn't send the file to trash, it's just gone.</p>
  
  <pre><code>var myFolder = DriveApp.getFolderById('3Bg2dKau456ySkhNBWB98W5sSTM');
  
  thisFile = myFolder.getFilesByName(myFileName);
  
  while (thisFile.hasNext()) {
    var eachFile = thisFile.next();
    var idToDLET = eachFile.getId();
    Logger.log('idToDLET: ' + idToDLET);
  
    var rtrnFromDLET = Drive.Files.remove(idToDLET);
  };
  </code></pre>
  
  <p>So, I'm combining the <code>DriveApp</code> service and the <code>DriveAPI</code> to delete the file without sending it to the trash.  The <code>DriveAPI</code> .remove needs the file ID, but I don't have the file ID, so the file gets looked up by name, then the file ID is retrieved, then the ID is used to delete the file.  So, if I can't find a way to overwrite the file, I can at least delete the old file without it going to the trash.</p>
  
  <p>I just noticed that the <code>DriveAPI</code> service has a <code>Patch</code> and an <code>Update</code> option.  </p>
  
  <pre><code>.patch(resource, fileId, optionalArgs)
  </code></pre>
  
  <p><a href="https://developers.google.com/drive/v2/reference/files/patch" rel="noreferrer">Google Documentation Patch</a>  Updates file metadata.</p>
  
  <p>The <code>resource</code> arg is probably the metadata.  The <code>fileId</code> is self explanatory.  I'm guessing that the <code>optionalArgs</code> are parameters that follow the HTTP Request Patch semantics?  I don't know.</p>
  
  <p>It looks like both Patch and Update will update data.  Update is a <code>PUT</code> request that will </p>
  
  <blockquote>
    <p>clears previously set data if you don't supply optional parameters.</p>
  </blockquote>
  
  <p>According to the documentation.  So it's safer to use a Patch request because any parameters that are missing are simply ignored.  I haven't tried it yet, but maybe this is the answer.</p>
  
  <p>I'm getting an error with <code>Patch</code>, so I'll try <code>Update</code>:</p>
  
  <pre><code>.update(resource, fileId, mediaData)
  </code></pre>
  
  <p>That has a arg for mediaData in the form of a blob.  And I think that is what I need.  But I'm not sure what the <code>resource</code> parameter needs.  So I'm stuck there.</p>