Remove all assets uploaded by the user in Firebase when user delete their account.
Knowledge

Remove all assets uploaded by the user in Firebase when user delete their account.

Learn how to Remove all assets uploaded by the user in Firebase when user delete their account.

Pankaj Sharma
Flutterflow development company
December 5, 2024
Table of content

Overview

When a user deletes their account, it is important to also remove all their associated assets stored in Firebase storage to maintain data integrity and ensure privacy. Here's an overview of how this process works in Firebase:

Prerequisites

  • flutterflow
  • Firebase

Flutterflow

FlutterFlow is a low-code development platform built on top of Flutter, Google's open-source UI toolkit for building natively compiled applications for mobile, web, and desktop from a single codebase.

To set up your Flutterflow project, please follow the steps below:

1. Create a Flutterflow project

  • Go to the Flutterflow console.
  • Click on Create a project and follow the instructions to set up a new project.

Firebase Project Setup

Firebase is a Google platform offering tools to build, improve, and scale apps across mobile, web, and desktop, with services like real-time databases, authentication, and serverless backend solutions.

To set up your Firebase project, please follow the steps below:

1. Create a firebase project

  • Go to the Firebase Console.
  • Click on Create a project and follow the instructions to set up a new project.

2. Enable Firebase Authentication

  • In the Firebase console, go to the Authentication section.
  • Click Get Started and enable the sign-in methods you need (e.g., Email/Password, Google)
  • Now copy the project ID and go in the settings of flutterflow project, Choose firebase project and paste the copied firebase project id in that and connect with firebase.

Delete User Account

If a user want to delete his account form the app then make sure his also remove all their associated assets from firebase storage. For we need to Create a custom action for that.

  • Profile Page : - User click on Delete Account button and call the below custom action which is delete the files which stored in firebase storage after that call delete account.

// Automatic FlutterFlow imports

import '/backend/backend.dart';

import '/flutter_flow/flutter_flow_theme.dart';

import '/flutter_flow/flutter_flow_util.dart';

import '/custom_code/actions/index.dart'; // Imports other custom actions

import '/flutter_flow/custom_functions.dart'; // Imports custom functions

import 'package:flutter/material.dart';

// Begin custom action code

// DO NOT REMOVE OR MODIFY THE CODE ABOVE!



import 'package:firebase_storage/firebase_storage.dart';



Future deleteUserFiles(String userId) async {

 // Add your function code here!

 try {

   // Reference to the user's folder

   final userFolderRef = FirebaseStorage.instance.ref('users/$userId/uploads');



   // List all files and subfolders in the user's folder

   final listResult = await userFolderRef.listAll();

   print("check length");

   print(listResult.items);

   // Delete all files

   for (var fileRef in listResult.items) {

     await fileRef.delete();

     print('Deleted file: ${fileRef.fullPath}');

   }



   // Recursively delete subfolders (if any)

   for (var folderRef in listResult.prefixes) {

     await deleteUserFiles(folderRef.fullPath);

   }



   print('User folder deleted successfully.');

 } catch (e) {

   print('Error deleting user folder: $e');

 }

}

The custom action taking only one argument that was userId. So in that you need to send document reference of the authenticated user on the click of delete Account button.

Conclusion

By structuring user assets logically and leveraging Firebase Authentication triggers and Cloud Functions, you can ensure that when a user deletes their account, all their associated assets are securely and efficiently deleted.

Remove all assets uploaded by the user in Firebase when user delete their account.

Ex- Technical Lead at Paytm Money | Senior Software Developer | NIT Surathkal

Flutterflow project image

Need expert help with Flutterflow and Firebase?

Contact Us
Flutterflow development company

View more blogs

Ready to develop your own product? Get in touch today!

Get in Touch  
Flutterflow app development
Whatsapp icon