nodejs-firestore icon indicating copy to clipboard operation
nodejs-firestore copied to clipboard

Add a dry run option to recursiveDelete

Open sushantdhiman opened this issue 3 years ago • 4 comments

Is your feature request related to a problem? Please describe. recursiveDelete is a dangerously destructive operation. I think there should be a way to list document(s) that will be deleted.

Describe the solution you'd like Add a third argument to recursiveDelete method, options?.dryRun: boolean

sushantdhiman avatar Aug 01 '22 10:08 sushantdhiman

I am using this alternative for now

(async () => {
  const users = [
    '7BHpq4x5epX7fXSJpd3XJbjb0b03',
    'AX4xmwEACNcHy9FMmX9MFmaTQXJ3',
    'CmQ2gPIxMLORjf8raKEszJ0lbNq2',
    'ccwj6u2CQzbuq4OQmSPvoVXxCSO2',
  ];

  const bulkWriter = firestore.bulkWriter();

  bulkWriter.delete = async (ref) => {
    console.log(ref.path);
    return { writeTime: Timestamp.fromDate(new Date()), isEqual: () => false };
  };

  for (const uid of users) {
    const doc = firestore.collection('users').doc(uid);
    await firestore.recursiveDelete(doc, bulkWriter);
  }

  process.exit(0);
})();

sushantdhiman avatar Aug 01 '22 10:08 sushantdhiman

Hi @sushantdhiman, thanks for your feature request.

Can you tell us more about your use case for listing all documents that will be deleted in a recursive delete? What kind of user is performing the recursive delete? And in what context are they performing the delete? Is the path the only information that is relevant to the user that is about to perform the recursiveDelete?

Thanks, Mark

MarkDuckworth avatar Aug 01 '22 23:08 MarkDuckworth

Hi,

Please find the answers for your questions below :-

Can you tell us more about your use case for listing all documents that will be deleted in a recursive delete?

Given dangerous consequence of performing recursiveDelete, I just want to be sure what document(s) are going to be deleted. For my use-case (code posted in earlier comment), I was not sure if this operation will keep other documents in users collection. Am I mistakenly going to delete all the users.

What kind of user is performing the recursive delete? And in what context are they performing the delete?

Developer (with GCP Project Admin access). For my context, I was just pruning database of some disabled accounts with a maintenance script.

Is the path the only information that is relevant to the user that is about to perform the recursiveDelete?

In my case number of parent / target documents were small, so yeah listing them was enough to be sure that not all the users will be deleted. For any case, I just want to be sure that I am not going to delete something unexpected by mistake.

Perhaps tree like structure (with depth control) can be used for this. In some cases just showing count of documents to be deleted should enough. Even just displaying the full path of parent document could be enough. An example output could be like this :-

.
├── 7BHpq4x5epX7fXSJpd3XJbjb0b03/
│   └── purchases/
│       ├── pX7fXS
│       └── wEACN
├── AX4xmwEACNcHy9FMmX9MFmaTQXJ3/
│   └── purchases/
│       └── PvoVXx
├── CmQ2gPIxMLORjf8raKEszJ0lbNq2/
│   └── invites/
│       └── jf8raKEszJ0lbNq2
└── ccwj6u2CQzbuq4OQmSPvoVXxCSO2

Total document: 8

sushantdhiman avatar Aug 02 '22 12:08 sushantdhiman

I appreciate the additional details on this feature request. I've put this request in our backlog for scheduling in a future sprint. In the meantime, please follow up with any additional questions or concerns.

(b/241146361)

MarkDuckworth avatar Aug 02 '22 21:08 MarkDuckworth