title: How to move a folder in Google Drive using Google Apps Script
body: <h3>EDIT</h3>
  <p>I have logged an issue with Google's Issue Tracker about the <code>Folder.moveTo(destination)</code> function not working, as it appears to be a bug. Feel free to star the issue to give it a bump!</p>
  <p><a href="https://issuetracker.google.com/issues/163080678" rel="nofollow noreferrer">https://issuetracker.google.com/issues/163080678</a></p>
  <hr />
  <h3>Background</h3>
  <p>I am trying to provide a &quot;one click&quot; way of moving folders in Google Drive via an add-on (which appears in both Drive and Gmail). For context, each &quot;folder&quot; in this case is a matter, and the &quot;one click&quot; will move the folder (containing all the relevant documents to that matter) from the &quot;open&quot; folder to the &quot;closed&quot; folder. However, I am getting confusing and unhelpful errors every way I try to achieve this.</p>
  <p><strong>Note</strong>: all of these folders are in a Shared Team Drive. I'm aware that this places some limitations on what the DriveApp Service and Drive API can do, but I'm crossing all my fingers and toes that it's not just an inherent limitation and that there is a workaround!</p>
  <p>I have already read the following:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/43595509/how-can-i-use-google-apps-script-to-move-files-between-personal-google-drives-an">How can I use Google Apps Script to move files between personal Google Drives and Team Drives?</a>.</li>
  <li><a href="https://stackoverflow.com/questions/30240322/google-script-move-folders">Google Script Move Folders</a></li>
  <li><a href="https://stackoverflow.com/questions/28294098/move-a-google-drive-folder-to-a-new-location-using-gas">Move a Google Drive folder to a new location using GAS</a></li>
  <li><a href="https://tanaikech.github.io/2019/11/20/moving-file-to-specific-folder-using-google-apps-script/" rel="nofollow noreferrer">https://tanaikech.github.io/2019/11/20/moving-file-to-specific-folder-using-google-apps-script/</a></li>
  <li><a href="https://developers.google.com/apps-script/reference/drive/folder#movetodestination" rel="nofollow noreferrer">https://developers.google.com/apps-script/reference/drive/folder#movetodestination</a></li>
  <li><a href="https://developers.google.com/drive/api/v2/reference/files/update" rel="nofollow noreferrer">https://developers.google.com/drive/api/v2/reference/files/update</a></li>
  <li><a href="https://developers.google.com/drive/api/v2/reference/parents/insert" rel="nofollow noreferrer">https://developers.google.com/drive/api/v2/reference/parents/insert</a></li>
  </ul>
  <p>But they did not solve my problem (noting that <code>addFile()</code>, <code>addFolder()</code>, <code>removeFile()</code> and <code>removeFolder()</code> have been <a href="https://developers.google.com/apps-script/reference/drive/folder#deprecated-methods" rel="nofollow noreferrer">deprecated</a> recently and so are no longer available as the answers above would suggest).</p>
  <hr />
  <h3>Expected Result</h3>
  <p>My expectation is that when the user hits the &quot;Close&quot; button in the add-on, it will move the relevant folder (selected at a previous stage in the add-on) from the &quot;open&quot; folder, to the &quot;closed&quot; folder (which sit next to each other at the same level in the folder hierarchy). To be clear, I'm hoping that it will <em>move</em> the folder, not duplicate it in the target folder and then delete the original from the original parent folder.</p>
  <h3>Actual Result</h3>
  <p>See specific error messages below, but generally speaking, when the user clicks the &quot;Close&quot; button, I get an error right away, and nothing happens to the folder.</p>
  <hr />
  <h3>Attempt 1 - Folder.moveTo(destination)</h3>
  <p>Taking the Google <a href="https://developers.google.com/apps-script/reference/drive/folder#movetodestination" rel="nofollow noreferrer">documentation</a> at face-value, I first attempted to use <code>Folder.moveTo(destination)</code> as below:</p>
  <pre><code>function moveFolder() {
      var folderToMove = DriveApp.getFolderById(MOVE_FOLDER_ID);
      var destinationFolder = DriveApp.getFolderById(DESTINATION_FOLDER_ID);
  
      folderToMove.moveTo(destinationFolder);
  }
  </code></pre>
  <p>However, on running this, I get the error message: <code>Exception: Unexpected error while getting the method or property moveTo on object DriveApp.Folder.</code></p>
  <h3>Attempt 2 - Drive.Files.update()</h3>
  <p>Failing to fix the above, I tried the <a href="https://developers.google.com/apps-script/advanced/drive" rel="nofollow noreferrer">Advanced Drive</a> feature of Google Apps Script, and attempted to use <code>Drive.Files.update()</code> (documentation <a href="https://developers.google.com/drive/api/v2/reference/files/update" rel="nofollow noreferrer">here</a>). My code looks like this:</p>
  <pre><code>function moveFolder() {
      Drive.Files.update({parents: [{id: DESTINATION_FOLDER_ID}]}, MOVE_FOLDER_ID);
  }
  </code></pre>
  <p>I also tried a variation including the optional paramter <code>supportsAllDrives</code>, as below:</p>
  <pre><code>function moveFolder() {
      Drive.Files.update({supportsAllDrives: true, parents: [{id: DESTINATION_FOLDER_ID}]}, MOVE_FOLDER_ID);
  }
  </code></pre>
  <p>However, both variations give the error: <code>GoogleJsonResponseException: API call to drive.files.update failed with error: File not found: [fileId]</code>, where <code>[fileId]</code> is the actual Google Drive ID of the folder being <strong>moved</strong>.</p>
  <p>I have confirmed in both attempts 1 and 2, by using various other functions I know do work (like adding customer properties with <code>Drive.Properties</code> and renaming the folder using DriveApp), that the folders themselves are definitely being passed through correctly and are able to be manipulated programmatically. Ie, the folder definitely exists, and using the URL in the browser, can definitely be found using that Drive ID.</p>