Skip to content

Update Album Delete Endpoint to Remove All Related ImagesΒ #47

@abhishek-nexgen-dev

Description

@abhishek-nexgen-dev

πŸ“Œ Description

We need to update the album delete API endpoint so that when a user deletes an album:

  • βœ… The album gets deleted from the database
  • βœ… All images related to that album are also removed from the database
  • βœ… All images are also deleted from Cloudinary (where they are hosted)

πŸ“‚ Current Problem

  • When an album is deleted, the album gets removed.
  • But the images related to that album may still stay in the database or remain in Cloudinary.
  • This causes unnecessary storage use and data inconsistency.

βœ… What Needs to Be Done

  1. Find all images related to the album in the database using the album ID or reference.
  2. Delete those images from Cloudinary using their public IDs.
  3. Delete those image documents from the database.
  4. Finally, delete the album from the database.

🧠 Logic (Simple Steps):

// Example (pseudo-code)
const album = await Album.findById(albumId);
const images = await Image.find({ album: albumId });

// 1. Delete images from Cloudinary
for (let image of images) {
  await cloudinary.uploader.destroy(image.public_id);
}

// 2. Delete image records from DB
await Image.deleteMany({ album: albumId });

// 3. Delete album from DB
await Album.findByIdAndDelete(albumId);

πŸ§ͺ How to Test

Create an album and upload multiple images to it.

  • Then call the delete album endpoint.

  • βœ… Check if the album is removed from the DB.

  • βœ… Check if related images are deleted from both the DB and Cloudinary.

πŸ’‘ Additional Suggestions

  • Add error handling for:

  • Cloudinary failures (e.g., retry or log)

  • Missing images or album

  • Optional: Add a confirmation step or warning before permanent deletion (for UI).

  • Optional: Add logging for deleted files (filename, public ID, etc.)

⏰ Priority: High

This is important to prevent junk data and unnecessary Cloudinary usage, which can increase storage cost.

#Β£ πŸ” Related Endpoints

DELETE /api/v1/admin/album/delete/AlbumId=

DELETE /api/images (or wherever images are stored)

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions