删除 Cloud Storage 中的文件 (Android)

将文件上传至 Cloud Storage 后,您还可以删除它们。

删除文件

如需删除某个文件,请先为该文件创建引用。然后,对该引用调用 delete() 方法。

Kotlin

// Create a storage reference from our app
val storageRef = storage.reference

// Create a reference to the file to delete
val desertRef = storageRef.child("images/desert.jpg")

// Delete the file
desertRef.delete().addOnSuccessListener {
    // File deleted successfully
}.addOnFailureListener {
    // Uh-oh, an error occurred!
}

Java

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

// Create a reference to the file to delete
StorageReference desertRef = storageRef.child("images/desert.jpg");

// Delete the file
desertRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {
        // File deleted successfully
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Uh-oh, an error occurred!
    }
});

处理错误

导致删除文件时出错的原因有很多,包括文件不存在,或者用户不具备删除相应文件的权限。如需详细了解这些错误,请参阅文档的处理错误部分。