Google drive app

Google Apps Script to create and delete folder

First of all, I would like to tell you this article is about Google Apps Script. Here, I am going to show you here how you can use Google apps script to create and delete folders from Drive.

Firstly, create a new folder in the root folder of Google drive

So, the following function creates a new folder name ‘New Folder‘ in the root directory of the Google Drive.

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] function createFolder() { DriveApp.createFolder(‘New Folder’); } [/cc]

Secondly, create a sub-folder inside an existing folder of Google drive.

Because the following function can be used to create a subfolder inside an existing folder.

Therefore, it accesses the parent folder using folder id and then creates a subfolder inside that.

So, you can use this method If you have parent folder.

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] function createSubFolder(){ var folder=DriveApp.getFolderById(‘1OLVKsgp5AY0rCgRYPTsHPB40qqG1dTs1’); folder.createFolder(‘Sub Folder’); } [/cc]

Thirdly, create a sub-folder inside an existing folder of Google drive if you know the name of the folder

Google Drive you can have multiple folders or files with same names, so if you use App Script to get folder using folder name then you will get collection of all folders which are having same name.

The following function get a collection of all folders having same names ‘New Folder‘.

Loop through each folder and create sub folder inside that

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] function createSubFolder() { var folders=DriveApp.getFoldersByName(‘New Folder’); while(folders.hasNext()){ var folder=folders.next(); folder.createFolder(‘Sub Folder’); } } [/cc]

Delete the folder from google drive (Send to Trash)

The below code snippet delete the folder from Google Drive.

“Please not that it just send the folder to Trash, it does not delete the folder permanently.”

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] function deleteFolder() { var folder=DriveApp.getFolderById(’12wY7rnnkBhXFm9ByTytECqCOdNjEBeRA’); folder.setTrashed(true); } [/cc]

Remove folder from google drive.

This method does not delete the folder. Because it just removes the folder from all of its parents. So it cannot be seen in Drive except by searching for it or using the “All items” view.

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] function deleteFolder() { var folder=DriveApp.getFoldersByName(‘TestFolder’).next(); DriveApp.removeFolder(folder); } [/cc]

Send all folders with same names to trash

This code block deletes all folders in the root folder which are having same name ‘New Folder

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] function deleteFolders() { var folders=DriveApp.getFoldersByName(‘New Folder’); while(folders.hasNext()){ var folder=folders.next(); folder.setTrashed(true); } } [/cc]

Permanently deleting any folder or file from google drive

You cannot delete file or folders permanently from Google Drive without activating advance Google services.

Therefore,  you need to enable the Drive API in the Google Cloud Platform first before deleting file or folder.
Likewise, the same method is used, whether you want to remove a file or folder. Certainly, you just need to pass the file id, if you need to remove the file or pass the folder id  if you need to remove the folder.”

[cc lang=”javascript” escaped=”true” width=”100%” theme=”blackboard” noborder=”1″ line_numbers=”off”] //Permanently Delete file from drive function removeFile(){ var fileId=’1etSGF3Op1hwlITUBKo9DRmHA2g5Ezl32uENzxOssokM’ Drive.Files.remove(fileId); } //Permanently delete folder from drive function removeFolder(){ var folderId=’1HEWiaLCG74bumbbx2AxHNGMVI2G4PSU’ Drive.Files.remove(folderId); } [/cc]

Enable Drive API:

If you have any query about Google apps script. Then you can use this phrase “google apps script comment” in the comment section and get a quick response.

2 Responses

Add a Comment

Your email address will not be published. Required fields are marked *