Delete files in azure

when I delete a form, I need to delete the corresponding files uploaded in azure also.
I have written a code like,

Once the form is saved, the file will delete in local path and uploaded to azure. Once the form is deleted the files should also be deleted in azure also.

Files are those uploaded in that form

Is there any solutions?

hii @SUKUMARAN_BV

You can delete an uploaded file in Azure using their file_id.
I was creating this type of custom app for when I try to upload a file it will automatically upload to Google Drive, and when I try to delete that attachment, it will automatically be deleted in Google Drive. Here is the file delete controller code you can refer to:

def delete_from_gd(doc, method=None):
	"""Delete file from Google drive"""
	if doc.file_url:
		# Extract file ID from the Google Drive link
		file_id = doc.file_url.split('/')[-2]
		delete_from_drive(file_id)

def delete_from_drive(file_id):
	"""
	Deletes a file from Google Drive using its file ID.
	"""
	google_settings_doc = frappe.get_doc("Google Settings", "Google Settings")
	creds = Credentials(
		None,
		refresh_token=frappe.db.get_value("Google Drive", "Google Drive", "refresh_token"),
		token_uri="https://oauth2.googleapis.com/token",
		client_id=google_settings_doc.get("client_id"),
		client_secret=google_settings_doc.get_password("client_secret")
	)
	service = build("drive", "v3", credentials=creds)

	try:
		service.files().delete(fileId=file_id).execute()
	except Exception as e:
		frappe.msgprint("Error in google drive file delete")

I hope it’s help for you
Thanks

when attachments is deleted, I wrote the code for deleting in azure also. I need once the form is deleted the corresponding file uploads in that form should be deleted in azure also

You can write an on_trash method to get all files linked with a particular document and then delete them.

1 Like

thank you