public async Task <IActionResult> DeleteGroup(string id, int groupId) { AppUser user = await userManager.FindByIdAsync(id); Group group = repository.Groups.FirstOrDefault(g => g.GroupId == groupId); if (user != null) { //delete group chat if exists if (group.GroupChatId != 0) { repository.DeleteGroupChat(group.GroupChatId); } //delete notifications foreach (Notification notification in repository.Notifications .Where(n => n.For == "Group" && n.ForId == groupId)) { repository.DeleteNotification(notification.NotificationId); } //delete photo and group directory if (group.GroupPhotoPath != null) { string dirPath = Path.Combine(hostingEnvironment.WebRootPath, $@"UsersData\Groups\{group.GroupId}"); System.IO.Directory.Delete(dirPath, true); } repository.DeleteGroup(groupId); return(RedirectToAction("Groups", new { id })); } else { return(RedirectToAction("UserNotFound", "Error")); } }