pcloud/file/
delete.rs

1use super::{File, FileIdentifier, FileResponse};
2
3impl crate::Client {
4    /// Deletes a file from pCloud.
5    ///
6    /// This function calls the `deletefile` endpoint to remove the specified file from the user's pCloud storage.
7    /// It returns the metadata of the deleted file upon success.
8    ///
9    /// # Arguments
10    ///
11    /// * `identifier` - A type convertible into a [`FileIdentifier`] that identifies the file to delete
12    /// (e.g., by file ID or path).
13    ///
14    /// # Returns
15    ///
16    /// A [`File`] struct containing metadata about the deleted file.
17    ///
18    /// # Errors
19    ///
20    /// Returns a [`crate::Error`] if the file does not exist or the API request fails.
21    ///
22    /// # Examples
23    ///
24    /// ```rust,no_run
25    /// # async fn example(client: &pcloud::Client) -> Result<(), pcloud::Error> {
26    /// let deleted_file = client.delete_file("myfolder/myfile.txt").await?;
27    /// println!("Deleted file ID: {}", deleted_file.file_id);
28    /// # Ok(())
29    /// # }
30    /// ```
31    pub async fn delete_file(
32        &self,
33        identifier: impl Into<FileIdentifier<'_>>,
34    ) -> crate::Result<File> {
35        self.get_request::<FileResponse, _>("deletefile", identifier.into())
36            .await
37            .map(|res| res.metadata)
38    }
39}
40
41#[cfg(test)]
42mod http_tests {
43    use crate::{Client, Credentials};
44    use mockito::Matcher;
45
46    #[tokio::test]
47    async fn success() {
48        let mut server = mockito::Server::new_async().await;
49        let m = server
50            .mock("GET", "/deletefile")
51            .match_query(Matcher::AllOf(vec![
52                Matcher::UrlEncoded("access_token".into(), "access-token".into()),
53                Matcher::UrlEncoded("fileid".into(), "42".into()),
54            ]))
55            .with_status(200)
56            .with_body(
57                r#"{
58    "result": 0,
59    "metadata": {
60        "name": "C61EWBrr2sU16GM4.bin",
61        "created": "Sat, 24 Jul 2021 07:38:41 +0000",
62        "thumb": false,
63        "modified": "Sat, 24 Jul 2021 07:38:41 +0000",
64        "isfolder": false,
65        "isdeleted": true,
66        "fileid": 42,
67        "hash": 9403476549337371523,
68        "comments": 0,
69        "category": 0,
70        "id": "f5257731387",
71        "isshared": false,
72        "ismine": true,
73        "size": 10485760,
74        "parentfolderid": 1075398908,
75        "contenttype": "application\/octet-stream",
76        "icon": "file"
77    }
78}"#,
79            )
80            .create();
81        let client = Client::new(server.url(), Credentials::access_token("access-token")).unwrap();
82        let result = client.delete_file(42).await.unwrap();
83        assert_eq!(result.file_id, 42);
84        m.assert();
85    }
86}