Deleting data with image

Hi, I’m building a simple account registration app in Retool Cloud that collects general data and a profile photo. The data is saved to Retool Postgres, and currently the image column stores the photo as base64; when I delete a user, the DB row is removed but the file in Retool Storage remains. What’s the recommended pattern to ensure the storage file is deleted as part of the delete flow? Should I store the Retool Storage file ID/path (or public URL) at upload time and then call the Retool Storage “Delete files” action—deleting from storage first, then deleting the DB row? Any pointers or example configurations would be greatly appreciated.

2 Likes

Hey @Ferdi112 and welcome to the forum!

Yup, that's the way I'd approach it, store the Retool Storage Id in the same row with the profile foto. Your delete flow can be the following

  • A javascript that stores both the row primary key and the retool storage image id as constables
  • A postgres query using additional scope to delete the row
  • A retool storage query using additional scope to delete the image
  • The js triggers the postgres query to remove the delete the row, and on success, it will trigger the retool storage trigger.

Here's an example of the query:

// Assume these values come from your table selection
const userId = table1.selectedRow.id;              // Primary key of the user
const storageFileId = table1.selectedRow.file_id;  // Retool Storage file ID

deleteUserRow.trigger({
  additionalScope: { id: userId },
  onSuccess: async () => {
    if (storageFileId) {
      try {
        await deleteStorageFile.trigger({
          additionalScope: { fileId: storageFileId }
        });

        utils.showNotification({
          title: "Success",
          description: "User and file deleted successfully",
          type: "success"
        });

        await getUsers.trigger(); // Refresh table after deletion
      } catch (err) {
        utils.showNotification({
          title: "Warning",
          description: "User deleted but file removal failed",
          type: "warning"
        });
      }
    } else {
      utils.showNotification({
        title: "Success",
        description: "User deleted (no file to remove)",
        type: "success"
      });
      await getUsers.trigger();
    }
  },
  onFailure: () => {
    utils.showNotification({
      title: "Error",
      description: "Failed to delete user from database",
      type: "error"
    });
  }
});

Although you can make it much more simpler if you don't want all the potential fallback errors.

Let me know if the above is not quite what you're searching, happy to keep brainstorming.

Best,
Miguel

2 posts were split to a new topic: How to rename photo before uploading to Retool Storage

I think the above post is probably what you're looking for, @Ferdi112, but les know if you have any additional questions!