@@ -95,11 +95,33 @@ impl FileIO {
95
95
/// # Arguments
96
96
///
97
97
/// * 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" ) ]
98
99
pub async fn remove_all ( & self , path : impl AsRef < str > ) -> Result < ( ) > {
99
100
let ( op, relative_path) = self . inner . create_operator ( & path) ?;
100
101
Ok ( op. remove_all ( relative_path) . await ?)
101
102
}
102
103
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
+
103
125
/// Check file exists.
104
126
///
105
127
/// # Arguments
@@ -441,7 +463,15 @@ mod tests {
441
463
let file_io = create_local_file_io ( ) ;
442
464
assert ! ( file_io. exists( & a_path) . await . unwrap( ) ) ;
443
465
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 ( ) ;
445
475
assert ! ( !file_io. exists( & b_path) . await . unwrap( ) ) ;
446
476
assert ! ( !file_io. exists( & c_path) . await . unwrap( ) ) ;
447
477
assert ! ( file_io. exists( & a_path) . await . unwrap( ) ) ;
@@ -460,7 +490,7 @@ mod tests {
460
490
let file_io = create_local_file_io ( ) ;
461
491
assert ! ( !file_io. exists( & full_path) . await . unwrap( ) ) ;
462
492
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( ) ) ;
464
494
}
465
495
466
496
#[ tokio:: test]
0 commit comments