@@ -95,11 +95,33 @@ impl FileIO {
9595 /// # Arguments
9696 ///
9797 /// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
98+ #[ deprecated( note = "use remove_dir_all instead" , since = "0.4.0" ) ]
9899 pub async fn remove_all ( & self , path : impl AsRef < str > ) -> Result < ( ) > {
99100 let ( op, relative_path) = self . inner . create_operator ( & path) ?;
100101 Ok ( op. remove_all ( relative_path) . await ?)
101102 }
102103
104+ /// Remove the path and all nested dirs and files recursively.
105+ ///
106+ /// # Arguments
107+ ///
108+ /// * path: It should be *absolute* path starting with scheme string used to construct [`FileIO`].
109+ ///
110+ /// # Behavior
111+ ///
112+ /// - If the path is a file or not exist, this function will be no-op.
113+ /// - If the path is a empty directory, this function will remove the directory itself.
114+ /// - If the path is a non-empty directory, this function will remove the directory and all nested files and directories.
115+ pub async fn remove_dir_all ( & self , path : impl AsRef < str > ) -> Result < ( ) > {
116+ let ( op, relative_path) = self . inner . create_operator ( & path) ?;
117+ let path = if relative_path. ends_with ( '/' ) {
118+ relative_path. to_string ( )
119+ } else {
120+ format ! ( "{relative_path}/" )
121+ } ;
122+ Ok ( op. remove_all ( & path) . await ?)
123+ }
124+
103125 /// Check file exists.
104126 ///
105127 /// # Arguments
@@ -441,7 +463,15 @@ mod tests {
441463 let file_io = create_local_file_io ( ) ;
442464 assert ! ( file_io. exists( & a_path) . await . unwrap( ) ) ;
443465
444- file_io. remove_all ( & sub_dir_path) . await . unwrap ( ) ;
466+ // Remove a file should be no-op.
467+ file_io. remove_dir_all ( & a_path) . await . unwrap ( ) ;
468+ assert ! ( file_io. exists( & a_path) . await . unwrap( ) ) ;
469+
470+ // Remove a not exist dir should be no-op.
471+ file_io. remove_dir_all ( "not_exists/" ) . await . unwrap ( ) ;
472+
473+ // Remove a dir should remove all files in it.
474+ file_io. remove_dir_all ( & sub_dir_path) . await . unwrap ( ) ;
445475 assert ! ( !file_io. exists( & b_path) . await . unwrap( ) ) ;
446476 assert ! ( !file_io. exists( & c_path) . await . unwrap( ) ) ;
447477 assert ! ( file_io. exists( & a_path) . await . unwrap( ) ) ;
@@ -460,7 +490,7 @@ mod tests {
460490 let file_io = create_local_file_io ( ) ;
461491 assert ! ( !file_io. exists( & full_path) . await . unwrap( ) ) ;
462492 assert ! ( file_io. delete( & full_path) . await . is_ok( ) ) ;
463- assert ! ( file_io. remove_all ( & full_path) . await . is_ok( ) ) ;
493+ assert ! ( file_io. remove_dir_all ( & full_path) . await . is_ok( ) ) ;
464494 }
465495
466496 #[ tokio:: test]
0 commit comments