Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blocking operator): add remove_all api #2449

Merged
merged 8 commits into from
Jun 21, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions core/src/types/operator/blocking_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,49 @@ impl BlockingOperator {
Ok(())
}

/// Remove the path and all nested dirs and files recursively.
///
/// # Notes
///
/// We don't support batch delete now.
///
/// # Examples
///
/// ```
/// # use anyhow::Result;
/// # use futures::io;
/// # use opendal::BlockingOperator;
/// # fn test(op: BlockingOperator) -> Result<()> {
/// op.remove_all("path/to/dir")?;
/// # Ok(())
/// # }
/// ```
pub fn remove_all(&self, path: &str) -> Result<()> {
let meta = match self.stat(path) {
Ok(metadata) => metadata,

Err(e) if e.kind() == ErrorKind::NotFound => return Ok(()),

Err(e) => return Err(e),
};

if meta.mode() != EntryMode::DIR {
return self.delete(path);
}

let mut obs = self.scan(path)?;

let _ = obs.try_for_each(|v| match v {
Ok(entry) => self.delete(entry.path()),
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
Err(e) => Err(e),
})?;

// Remove the directory itself.
self.delete(path)?;

Ok(())
}

/// List current dir path.
///
/// This function will create a new handle to list entries.
Expand Down