public ResponseData DeleteMaterial([FromBody] Material material) { String lang = GetLanguage(); var entity = _db.Materials.FirstOrDefault(item => item.Id == material.Id); // If image exist, try delete it if (entity != null && entity.ImagePath != null) { String filePath = Path.Combine( Path.Combine(_environment.WebRootPath, _imageFolderPath), Path.GetFileName(entity.ImagePath) ); GC.Collect(); GC.WaitForPendingFinalizers(); System.IO.File.Delete(filePath); } // Remove material from database try { _db.Remove(_db.Materials.Single(a => a.Id == material.Id)); _db.SaveChanges(); } catch { }; return(new ResponseData() { reqType = "delete", material = material, messages = new List <String>() { (String)_translate.materialDeleteSuccess[lang] }, // Success : Material was deleted isError = false }); }