---
title: "Discussion on \"How to copy, delete, or move a document in Firestore\" | Hashnode"
description: "Discussion on \"How to copy, delete, or move a document in Firestore\". To move a Firestore document from one collection to another, including all existing sub-collections, we will need a Cloud function that copies all documents and then delete existing ones. In this post, you'll find working functions to use with some e..."
url: "https://hashnode.com/posts/firestore-move/5fb19e0b03f7294d0e3a8ab9"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How to copy, delete, or move a document in Firestore](https://leechy.dev/firestore-move?utm_source=hashnode&utm_medium=feed)

To move a Firestore document from one collection to another, including all existing sub-collections, we will need a Cloud function that copies all documents and then delete existing ones. In this post, you'll find working functions to use with some e...

Nov 15, 2020 · 6 min read · leechy.dev  
Read the article

-   [#firestore](https://hashnode.com/tag/firestore)

## Written by

[Andrey Lechev](https://hashnode.com/@leechy)

@leechy

  
[View profile](https://hashnode.com/@leechy)

Some frontender thoughts

## Responses 3

Markdown supported · ⌘↵ to post

[Maciej Węgrzynek](https://hashnode.com/@MaciejW)

[Feb 16, 2022](https://hashnode.com/posts/firestore-move/5fb19e0b03f7294d0e3a8ab9/comment/620ced7625673f2740eea0df)

For "Copy" script, shouldn't there be

```
// get all the documents in the collection
await subcollectionRef
```

instead of

```
// get all the documents in the collection
 return await subcollectionRef
```

when copying recursively?

Return value in for..of means that iterator will finish. So only the first subcollection will be copied. Or maybe I'm missing something.

[Sam Smith](https://hashnode.com/@goldyy1)

[Dec 13, 2021](https://hashnode.com/posts/firestore-move/5fb19e0b03f7294d0e3a8ab9/comment/61b77c0a0059f90a57a57530)

You should include your thesis statement into your introduction paragraph. It should unite all your arguments [https://educibly.com/](https://educibly.com/) . Here are some main tips that will help you deal with such specific paper as 5 paragraph essay.

[Thomas Götzsche](https://hashnode.com/@tgotzsche)

[Jul 30, 2021](https://hashnode.com/posts/firestore-move/5fb19e0b03f7294d0e3a8ab9/comment/61045bbe3b9b85399dcf1a84)

shouldn't at least the moveDoc function wrap the operations in a transaction?

[Andrey Lechev](https://hashnode.com/@leechy)

[Jul 31, 2021](https://hashnode.com/posts/firestore-move/5fb19e0b03f7294d0e3a8ab9/comment/61045bbe3b9b85399dcf1a84#reply-6105a5373b9b85399dcf3a52)

Seems strange that you are going to delete document that can be changed during the operation.

But even in that case the read operation is done once and the newly created document will have the content at this moment. No transaction needed.

And if youare expecting the write operation in the result document... all of the content will be overwritten.

Generally, make sure, that you are not working with actively used documents. Otherwise a whole bunch of errors can happen: loss of data, update of non-existing documents and so on. Or... change the functions to handle all the cases: only copy, but not move and create the new doc at least with merge option, but better check if the doc exists and merge the data with some custom logic.
