0

I want to Empty Share with me folder How can I empty It is My Code and I am using Google Drive V3 .

service.Permissions.Delete(PermissionID, fileId).Execute();
service.Files.Delete(fileId).Execute(); 

Both line gives a permission 403 error.

If I delete MyDrive file that time Second line he worked fine but Shared With me Folder not Deleted

1

1 Answer 1

0

The thing you need to remember is that Share with me is not a file you actually own that is why this didn't work.

service.Files.Delete(fileId).Execute();

First Get a list of all files in Share with me folder.

var request = service.Files.List();
request.Q = "(sharedWithMe = true)";
request.Fields = "*";
var results = request.Execute();

Find the file you wish to delete:

var myfile = results.Files.Where(a => a.Name.ToLower().Equals("receipt.pdf")).FirstOrDefault();

Now find the permissions on that file associated with the current authenticated user:

var per = myfile.Permissions.Where(a => a.EmailAddress.ToLower().Equals("[email protected]")).FirstOrDefault();

Delete the permissions from the mail file.

service.Permissions.Delete(myfile.Id, per.Id).Execute();

I tested it and it worked. You can just run the initial request though a loop and delete everything if you wish.

Note: This does not appear to work in all cases. I have a file on my Google drive that was shared with me by what appears to be a service account. I have no permissions on the file there for i cant remove my access. I am still digging.

2

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.