I am trying to delete multiple files at once from my google drive using the REST API. I found this SO Question-bulk deletion Google Drive API, I have tried this solution without the npm package using fetch.
This is what I have at the moment.
const boundary = 'END_OF_PART';
const separation = `\n--'${boundary}'\n`;
const ending = `\n--'${boundary}'--`;
const requestBody = files.reduce((accum, current) => {
accum += separation +
'Content-Type: application/http\n\n' +
`DELETE https://www.googleapis.com/drive/v2/files/${current.id}
\nAuthorization: Bearer ${this.getToken()}`
return accum
}, '') + ending
const _multiPart = [];
files.forEach((file) => {
const obj = {
"Content-Type": "application/http",
body: `DELETE https://www.googleapis.com/drive/v2/files/${file.id}\n`,
};
_multiPart.push(obj);
});
const url = new URL('https://www.googleapis.com/batch/drive/v3');
const requestOptions = {
method: 'POST',
headers: {
"Content-Type": "multipart/mixed",
'Authorization' : `Bearer ${this.getToken()}`
},
body: requestBody,
multipart: _multiPart,
};
await fetch(url, requestOptions);
When I run the deletion I get the id's of the files but the end result is Error 400 bad request. Documentation is scarce for this process. What is it that I am doing wrong here and how can I fix it to get this to work?
End result that I am looking for is deleting a large amount of files from my google drive.
Can anyone point me in the right direction on this ?.
EDIT: I just tried a solution at this SO Question: Bulk delete files on Google Drive with raw XMLHttpRequest
And it did delete 100 files, but the response I got back was Code 500, Internal error.
Why am I getting this response and again why is my previous code not working at all ?