Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot delete files from firebase collection

I am following the example listed here, except with modifications due to the API of the new firebase-tools.

exports.clearMessages = functions.runWith({ timeoutSeconds: 540, memory: '2GB' }).https.onCall(messagesController.clearMessages)

export const clearMessages = async (data, context) => {
    const uid = context.auth.uid
    const path = `users/${uid}/messages`
    return firebase_tools.firestore.delete('flightApp3', path, {
        recursive: true,
        shallow: true,
        allCollections: true
    }).then(result => {
        console.log('delete result', result)
        return result
    })
}

However, when I run this , I see the following displayed in Cloud Functions log:

Unhandled error { Error
    at Error.FirebaseError (/user_code/node_modules/firebase-tools/lib/error.js:9:18)
    at module.exports (/user_code/node_modules/firebase-tools/lib/getProjectId.js:10:19)
    at Command.module.exports (/user_code/node_modules/firebase-tools/lib/requirePermissions.js:11:21)
    at /user_code/node_modules/firebase-tools/lib/command.js:154:38
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)
  name: 'FirebaseError',
  message: 'No project active. Run with \u001b[1m--project <projectId>\u001b[22m or define an alias by\nrunning \u001b[1mfirebase use --add\u001b[22m',
  children: [],
  status: 500,
  exit: 1,
  stack: 'Error\n    at Error.FirebaseError (/user_code/node_modules/firebase-tools/lib/error.js:9:18)\n    at module.exports (/user_code/node_modules/firebase-tools/lib/getProjectId.js:10:19)\n    at Command.module.exports (/user_code/node_modules/firebase-tools/lib/requirePermissions.js:11:21)\n    at /user_code/node_modules/firebase-tools/lib/command.js:154:38\n    at process._tickDomainCallback (internal/process/next_tick.js:135:7)',
  original: undefined,
  context: undefined }

However, I'm pretty sure I have an active project in my firebase CLI.

$ firebase use
Active Project: production (flightApp3)

Project aliases for /Users/myUser/Developer/flightApp3/cloud:

* default (flightApp3)
* production (flightApp3)

Run firebase use --add to define a new project alias.
like image 767
TIMEX Avatar asked Nov 07 '18 06:11

TIMEX


People also ask

How do I delete files from firestore collection?

To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them. If you have larger collections, you may want to delete the documents in smaller batches to avoid out-of-memory errors.

How do I delete files from Firebase?

To delete a file, first create a reference to that file. Then call the delete() method on that reference, which returns a Promise that resolves, or an error if the Promise rejects. Learn more about the tree-shakeable Web v9 modular SDK and upgrade from version 8.

How do I delete multiple files from Firebase storage?

There is no API for bulk or batch deleting files in Cloud Storage. You will have to delete each one individually as described in the documentation. This user is second on the weekly Google Cloud leaderboard. Save this answer.


1 Answers

some options cannot be mixed ...

return firebase_tools.firestore.delete('flightApp3', path, {
    // allCollections: true,
    recursive: true,
    yes: true
}).then(() => {
    return {
      path: path 
    };
});

that's how the path is being built up (path and allCollections also do not seem to make sense together): projects/${project}/databases/(default)/documents/users/${uid}/messages

getProjectId.js checks for rc.projects (where options.project is option --project):

module.exports = function(options, allowNull) {
    if (!options.project && !allowNull) {
        var aliases = _.get(options, "rc.projects", {});
        ...

these rc.projects are the projects from the .firebaserc file:

{
   "projects": {
        "default": "flightApp3"
    }
}

or run firebase use default to switch from alias production to default (or remove alias production once for a test). FirestoreDelete(project, path, options) also does not care about options.token nor options.project anymore (as the documentation suggests).


$ firebase firestore:delete --help explains the command-line options:

Usage: firestore:delete [options] [path]

Delete data from Cloud Firestore.

Options:

  -r, --recursive    Recursive. Delete all documents and sub-collections. 
                     Any action which would result in the deletion of child
                     documents will fail if this argument is not passed.

                     May not be passed along with --shallow.


  --shallow          Shallow. Delete only parent documents and ignore documents
                     in sub-collections. Any action which would orphan documents
                     will fail if this argument is not passed.

                     May not be passed along with --recursive.


  --all-collections  Delete all. Deletes the entire Firestore database,
                     including all collections and documents.

                     Any other flags or arguments will be ignored.


  -y, --yes          No confirmation. Otherwise, a confirmation prompt will appear.

the npm package (the output above) is at version 6.0.1.


just found a relevant comment (but possibly obsolete):

The token must be set in the functions config, and can be generated at the command line by running firebase login:ci.

this hints for environment configuration, so that functions.config().fb.token has the token:

firebase functions:config:set fb.token="THE TOKEN"

one can also obtain the projectId from process.env.FIREBASE_CONFIG.projectId.

like image 127
Martin Zeitler Avatar answered Sep 28 '22 09:09

Martin Zeitler