0

I have a collection which has a subcollection.I do not want to delete the collection,but I want to delete all subcollections of a particular document(i.e.currentuser doc) when user kills the app.

I was doing something like below from client side using services.But then that only deletes the collection documents data,it does not delete the subcollection:

class onAppKilled: Service() {
    private lateinit var mFirestore:FirebaseFirestore
    private var mAuth: FirebaseAuth? = null
    lateinit var currentUser: FirebaseUser
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }
    override fun onTaskRemoved(rootIntent: Intent?) {

        mFirestore= FirebaseFirestore.getInstance()
        mAuth = FirebaseAuth.getInstance()
        currentUser= mAuth!!.currentUser!!

        currentUser.delete().addOnCompleteListener(OnCompleteListener {task->
            if(task.isSuccessful){
                Toast.makeText(this@onAppKilled,"User Deleted", Toast.LENGTH_LONG).show()

            }else{
                Toast.makeText(this@onAppKilled,"User not deleted", Toast.LENGTH_LONG).show()

            }
        })
     mFirestore.collection("AllUsers").document(SplitString(currentUser.email!!)).delete()


    }

I read about using cloud functions for deleting subcollections.So i installed cloud functions with my app but now I am not sure how do I go about deleting the particular documents subcollection when the app is killed?

sansiad
  • 59
  • 1
  • 10
  • @AlexMamo But in that question,the answer is given for how to delete collection and subcollection using batch operations.But still I am not sure how to use those operations when my app is killed.How can I do it when my app is killed,thats onTaskRemoved. – sansiad Mar 15 '19 at 10:57
  • You should run that code when your app is killed, most likely in your `onDestroy()` function. – Alex Mamo Mar 15 '19 at 10:59
  • 1
    Okay.I will try it then. – sansiad Mar 15 '19 at 10:59
  • @AlexMamo Actually I typed the question wrong I want to delete all subcollections of a particular document(i,e currentuser).That code is for deleting entire collection right? – sansiad Mar 15 '19 at 11:27
  • In that case, you have to find every subcollection within that document, find all documents that corresponde to each subcollection and delete them. It's basically the same thing. But please note, that you cannot do this in a single go. – Alex Mamo Mar 15 '19 at 11:31
  • @AlexMamo Then how do I do this.Please could you give me an example.Do i use batch operations or cloud functions? – sansiad Mar 15 '19 at 11:33
  • You should use one or the other according to your use-case of your app. But even if you try to use Cloud Functions, same principle apply. You have to find all documents within a subcollection and delete them. – Alex Mamo Mar 15 '19 at 11:36
  • @AlexMamo I will try that code then,collectionref is my collection ref right and could you tell me what is the executor/executor value in this case? – sansiad Mar 15 '19 at 11:44
  • Please check Doug's answer from this [post](https://stackoverflow.com/questions/48358608/which-are-the-recomended-arguments-to-use-when-deleting-a-collection-within-a-cl). – Alex Mamo Mar 15 '19 at 11:50
  • @AlexMamo I can delete the collection documents data using batch operations but for subcollection its not deleting,how do I delete subcollection with that function.I am very confused.You mentioned to find docs that correspond to each subcollection,i tried doing that but its not working.What is the way to reach and delete the subcollections?Is there any example for subcollections? – sansiad Mar 15 '19 at 15:58
  • @AlexMamo Can I post this as a new question as this is closed and im not able to figure it out,tried and searched a lot...that answer is for collections,but for subcollections I cant understand nor did I find any related resource using batch operations for deleting subcollections in kotlin? – sansiad Mar 16 '19 at 10:27
  • Yes, please post another fresh question with what you have tried using a [MCVE](https://stackoverflow.com/help/mcve), so me and other Firebase developers can help you. – Alex Mamo Mar 16 '19 at 10:42
  • Okay,I will create a new question.Thank you. – sansiad Mar 16 '19 at 11:06
  • You're welcome. – Alex Mamo Mar 16 '19 at 11:08

0 Answers0