Trang này mô tả cách sử dụng SDK Cloud Storage for Firebase để xoá các tệp đã được tải lên một bộ chứa Cloud Storage.
Xoá tệp
Để xoá một tệp, trước tiên, hãy tạo tệp đối chiếu cho tệp đó. Sau đó, gọi phương thức deleteWithCompletion: trên tệp đối chiếu đó.
Swift
// Create a reference to the file to delete let desertRef = storageRef.child("desert.jpg") do { // Delete the file try await desertRef.delete() } catch { // ... }
Objective-C
// Create a reference to the file to delete FIRStorageReference *desertRef = [storageRef child:@"images/desert.jpg"]; // Delete the file [desertRef deleteWithCompletion:^(NSError *error){ if (error != nil) { // Uh-oh, an error occurred! } else { // File deleted successfully } }];
Xử lý lỗi
Có một số lý do khiến lỗi có thể xảy ra khi xoá tệp, bao gồm cả việc tệp không tồn tại hoặc người dùng không có quyền xoá tệp dự định. Để biết thêm thông tin về lỗi, hãy xem phần Xử lý lỗi.