sxyazi/yazi · error

restore target already exists

Error message

restore target already exists: {to:?}

What it means

restore_item restores a trashed file by first creating the target exclusively (create_dir for dirs, File::create_new for files) and then renaming the backing file into place. If the target already exists, the exclusive create fails with AlreadyExists, which is re-thrown with the clearer message "restore target already exists: {to:?}". The library refuses to overwrite existing files during restore.

Solutions

  1. Check the target path for existence before restoring and rename the incoming item (e.g. append " (1)") or move the existing one aside.
  2. Delete or move the conflicting target if it is safe to overwrite, then retry the restore.
  3. Restore to an alternative location by adjusting the item's original path before restoring.

Example fix

// before
fs::restore(entries).await?;
// after
for e in &entries {
    if let Some(to) = &e.original {
        if to.exists() {
            let alt = to.with_file_name(format!("{} (restored)", to.file_name().unwrap().to_string_lossy()));
            fs::rename(&e.backing, &alt).await?;
            continue;
        }
    }
}
fs::restore(entries).await?;
Defensive patterns

Strategy: validation

Validate before calling

// Rust
if to.exists() {
    let to = to.with_file_name(format!("{} (1)", to.file_name().unwrap().to_string_lossy()));
}

Try / catch

match trash::restore(entries).await {
    Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => prompt_overwrite_or_rename(),
    other => other?,
}

Prevention

When it happens

Trigger: Calling restore (yazi-fs trash restore) when a file or directory already exists at the recorded original path — e.g. a same-named file was recreated after deletion, or the item was deleted, restored once, and restored again.

Common situations: Restoring an old download whose filename was re-created; batch-restoring a directory that partially exists; concurrent restore operations from two instances.

Related errors


AI-assisted analysis of sxyazi/yazi@5f901b886b (2026-09-02). Data as JSON: /api/errors/4b67a0f2cbab9be4. Report an issue: GitHub.

Appendix: source

Thrown at yazi-fs/src/trash/common.rs:14

#[cfg(trash_unix)]
pub(super) fn restore_item(from: &std::path::Path, to: &std::path::Path) -> std::io::Result<()> {
	use std::{fs, io};

	let is_dir = fs::symlink_metadata(from)?.is_dir();
	if let Some(parent) = to.parent() {
		fs::create_dir_all(parent)?;
	}

	match if is_dir { fs::create_dir(to) } else { fs::File::create_new(to).map(|_| ()) } {
		Ok(()) => fs::rename(from, to),
		Err(e) if e.kind() == io::ErrorKind::AlreadyExists => Err(io::Error::new(
			io::ErrorKind::AlreadyExists,
			format!("restore target already exists: {to:?}"),
		)),
		Err(e) => Err(e),
	}
}

View on GitHub (pinned to 5f901b886b)