Delete Supabase bucket images from Custom Action. Enjoy !!!

Hello:

Here is my Custom Action code for deleting storaged images in a supabase bucket.

// Automatic FlutterFlow imports
import '/backend/schema/structs/index.dart';
import '/backend/supabase/supabase.dart';
import '/actions/actions.dart' as action_blocks;
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:supabase/supabase.dart';

final supabase = SupabaseClient('YOUR_API_URL','YOUR_API_KEY');

Future<void> deleteImageFromBucket(String? imageUrl) async {
  if (imageUrl == null) {
    print('URL de imagen no proporcionada');
    return; // Salir de la función si la URL de la imagen es nula
  }
  try {
    // Extraer el nombre del archivo de la URL
    final List<String> urlParts = imageUrl.split('/');
    final String imageName = urlParts.last;

    final response = await supabase.storage.from('YOUR_BUCKET_NAME').remove(
        ['YOUR_BUCKET_FOLDER/$imageName']); // Especificar la ruta completa de la imagen
    if (response.isEmpty) {
      print('Imagen borrada con éxito');
      // Realizar cualquier otra acción después de eliminar la imagen
    } else {
      print('Error al borrar imagen');
      // Manejar el error según sea necesario
    }
  } catch (e) {
    print('Error: $e');
    // Manejar el error según sea necesario
  }
}

Replace your YOUR_API_URL and YOUR_API_KEY in code.
Replace YOUR_BUCKET_NAME and YOUR_BUCKET_FOLDER in code.
Pass imageUrl as imagePath parameter.

Enjoy !!!

Note: I assume it will work for any type of file stored in a bucket but I have only tested it with image type files.

3
1 reply