I'm trying to build a feature that deletes old files in a shared Google drive folder as follows:
private async deleteExpiredFiles(
dataType: DataType,
folderId: string,
expirationDate: string,
): Promise<void> {
const {
data: { files: filesToDelete },
} = await this.drive.files.list({
q: `'${folderId}' in parents and createdTime <= '${expirationDate}'`,
supportsAllDrives: true,
includeItemsFromAllDrives: true,
});
if (!filesToDelete) {
return;
}
await Promise.all(
filesToDelete.map(async (file) => {
if (!file.id) {
return;
}
const permissionIds = await this.drive.permissions.list({
fileId: file.id,
supportsAllDrives: true,
});
if (!permissionIds.data.permissions) {
return;
}
Promise.all(
permissionIds.data.permissions.map(async (permission) => {
if (!permission.id || !file.id) {
return;
}
await this.drive.permissions.delete({
fileId: file.id,
permissionId: permission.id,
supportsAllDrives: true,
});
}),
);
}),
);
}
I have set up all the permission in the Google Drive folder as well as get the correct credentials for the service account. However when I try to run this, I get the following error: GaxiosError: The authenticated user does not have the required access to delete the permission.
I can use drive.files.list fine and see the files as well as list the permission ids. But it isn't letting me delete them. Any advice?
Solved!
Rather than permanent deleting the files. I simply move them to the trash wth the following code:
await this.drive.files.update({
fileId: file.id,
requestBody: { trashed: true },
supportsAllDrives: true,
});