The docs say:
Deleting collections from a Web client is not recommended.
And thus omit to give an example (although a Node.js example is provided).
So you can either tweak the Node.js code to work in JavaScript (although this is not recommended by Google) OR you can use the cloud function solution that Google provide (if you are using cloud functions).
Delete collection with a Callable Cloud Function
Here's the cloud function code straight from the docs:
CLOUD FUNCTION CODE
/**
* Initiate a recursive delete of documents at a given path.
*
* The calling user must be authenticated and have the custom "admin" attribute
* set to true on the auth token.
*
* This delete is NOT an atomic operation and it's possible
* that it may fail after only deleting some documents.
*
* @param {string} data.path the document or collection path to delete.
*/
exports.recursiveDelete = functions
.runWith({
timeoutSeconds: 540,
memory: '2GB'
})
.https.onCall(async (data, context) => {
// Only allow admin users to execute this function.
if (!(context.auth && context.auth.token && context.auth.token.admin)) {
throw new functions.https.HttpsError(
'permission-denied',
'Must be an administrative user to initiate delete.'
);
}
const path = data.path;
console.log(
`User ${context.auth.uid} has requested to delete path ${path}`
);
// Run a recursive delete on the given document or collection path.
// The 'token' must be set in the functions config, and can be generated
// at the command line by running 'firebase login:ci'.
await firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
force: true,
token: functions.config().fb.token
});
return {
path: path
};
});
CLIENT SIDE CODE
/**
* Call the 'recursiveDelete' callable function with a path to initiate
* a server-side delete.
*/
function deleteAtPath(path) {
var deleteFn = firebase.functions().httpsCallable('recursiveDelete');
deleteFn({ path: path })
.then(function(result) {
logMessage('Delete success: ' + JSON.stringify(result));
})
.catch(function(err) {
logMessage('Delete failed, see console,');
console.warn(err);
});
}
As always, why is GCP so difficult!