FiringBlanks
FiringBlanks

Reputation: 2084

Clear Firestore database of all data?

After over a year of development, I have a pretty well developed site. Now I'm going to open it up for a public beta but I need to clear my Firestore of all it's data (it's all test data). Is there a simple way to do this or do I have to delete every single entry by hand? (There are over 1000 entries). The only realistic option I can think of at the moment is to switch Firebase projects to a new one, which is not something I want to do.

Solution: After reaching out to Firebase support, I was given the following CLI command to run, which did the trick:

// THIS WILL PROMPT CONFIRMATION AND WILL USE ACTIVE PROJECT
firebase firestore:delete --all-collections

// !!! WARNING !!! THIS WILL NOT PROMPT CONFIRMATION AND WILL USE ACTIVE PROJECT
firebase firestore:delete --all-collections -y

Remember to use caution, as this will wipe your entire Firestore database.

Upvotes: 22

Views: 3177

Answers (2)

Neha Nagda
Neha Nagda

Reputation: 1

i have developed my wesite using react and redux-toolkit, and to clear all collections, i wrote this code. I write collection name in this input and it deletes all the documents of collection, but i still have my collection, so my all data is gone and i don't need to make any change in my code

const [collectionName, setCollectionName] = useState("");
<form>
    <input type="text" onChange={(e) => setCollectionName(e.target.value)} />
    <span
    onClick={() => dispatch(deleteDocuments(collectionName))}
    >
    Delete documents
    </span>
</form>

export const deleteDocuments = createAsyncThunk(
"deleteDocuments",
async (collectionName, thunkAPI) => {
    try{
    console.log(collectionName);
    const querySnapshot = await getDocs(collection(db, collectionName));
    querySnapshot.forEach(async (testplan) => {
        console.log(testplan);
        await deleteDoc(doc(db, collectionName, testplan.id));
    });
    console.log('all docments successfully deleted');
    } catch (e){
    console.log(e);
    }
}
);

Upvotes: 0

Philip Su
Philip Su

Reputation: 113

According to OP and the Firebase team, the following CLI commands will do the trick:

// THIS WILL PROMPT CONFIRMATION AND WILL USE ACTIVE PROJECT
firebase firestore:delete --all-collections

// !!! WARNING !!! THIS WILL NOT PROMPT CONFIRMATION AND WILL USE ACTIVE PROJECT
firebase firestore:delete --all-collections -y

And to answer the OP's other question, here's how you delete an entire collection in Firestore with one click: Firebase console menu. You basically click the three-button context menu on the upper left of any collection and choose Delete Collection.

[Posting this to address @Nathan's feedback, so that this popular question actually gets an official marked "answer"]

Upvotes: 2

Related Questions