Parents: delete

Stay organized with collections Save and categorize content based on your preferences.

Removes a parent from a file. Try it now or see an example.

Request

HTTP request

DELETE https://www.googleapis.com/drive/v2/files/fileId/parents/parentId

Parameters

Parameter name Value Description
Path parameters
fileId string The ID of the file.
parentId string The ID of the parent.
Optional query parameters
enforceSingleParent boolean Deprecated. If an item is not in a shared drive and its last parent is deleted but the item itself is not, the item will be placed under its owner's root. (Default: false)

Authorization

This request requires authorization with at least one of the following scopes:

Scope
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file

Some scopes are restricted and require a security assessment for your app to use them. For more information, see the authentication and authorization page.

Request body

Do not supply a request body with this method.

Response

If successful, this method returns an empty response body.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library.

import com.google.api.services.drive.Drive;

import java.io.IOException;
// ...

public class MyClass {

  // ...

  /**
   * Remove a file from a folder.
   *
   * @param service Drive API service instance.
   * @param folderId ID of the folder to remove the file from.
   * @param fileId ID of the file to remove from the folder.
   */
  private static void removeFileFromFolder(Drive service, String folderId,
      String fileId) {
    try {
      service.parents().delete(fileId, folderId).execute();
    } catch (IOException e) {
      System.out.println("An error occurred: " + e);
    }
  }

  // ...

}

.NET

Uses the .NET client library.

using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;

using System.Net;
// ...

public class MyClass {

  // ...

  /// <summary>
  /// Remove a file from a folder.
  /// </summary>
  /// <param name="service">Drive API service instance.</param>
  /// <param name="folderId">ID of the folder to remove the file from.</param>
  /// <param name="fileId">ID of the file to remove from the folder.</param>
  public static void RemoveFileFromFolder(DriveService service,
      String folderId, String fileId) {
    try {
      service.Parents.Delete(fileId, folderId).Execute();
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
    }
  }

  // ...
}

PHP

Uses the PHP client library.

/**
 * Remove a file from a folder.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @param String $folderId ID of the folder to remove the file from.
 * @param String $fileId ID of the file to remove from the folder.
 */
function removeFileFromFolder($service, $folderId, $fileId) {
  try {
    $service->parents->delete($fileId, $folderId);
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
}

Python

Uses the Python client library.

from apiclient import errors
# ...

def remove_file_from_folder(service, folder_id, file_id):
  """Remove a file from a folder.

  Args:
    service: Drive API service instance.
    folder_id: ID of the folder to remove the file from.
    file_id: ID of the file to remove from the folder.
  """
  try:
    service.parents().delete(fileId=file_id, parentId=folder_id).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error

JavaScript

Uses the JavaScript client library.

/**
 * Remove a file from a folder.
 *
 * @param {String} folderId ID of the folder to remove the file from.
 * @param {String} fileId ID of the file to remove from the folder.
 */
function removeFileFromFolder(folderId, fileId) {
  var request = gapi.client.drive.parents.delete({
    'parentId': folderId,
    'fileId': fileId
  });
  request.execute(function(resp) { });
}

Go

Uses the Go client library.

import (
  "google.golang.org/drive/v2"
  "fmt"
)

// RemoveParentFromFile removes the given file from the given folder
func RemoveParentFromFile(d *drive.Service, folderId string, fileId string) error {
  err := d.Parents.Delete(fileId, folderId).Do()
  if err != nil {
    fmt.Printf("An error occurred: %v\n", err)
    return err
  }
  return nil
}

Objective-C

Uses the Objective-C client library.

#import "GTLDrive.h"
// ...

+ (void)removeFileFromFolderWithService:(GTLServiceDrive *)service
                               folderId:(NSString *)folderId
                                 fileId:(NSString *)fileId {
  GTLQueryDrive *query =
    [GTLQueryDrive queryForParentsDeleteWithFileId:fileId parentId:folderId];
  // queryTicket can be used to track the status of the request.
  GTLServiceTicket *queryTicket =
    [service executeQuery:query
        completionHandler:^(GTLServiceTicket *ticket, id object,
                            NSError *error) {
          if (error != nil) {
            NSLog(@"An error occurred: %@", error);
          }
        }];
}

// ...

Try it!

Use the APIs Explorer below to call this method on live data and see the response.