Overview
This node performs various file system operations on files and directories. It is useful for automating common file management tasks such as cleaning up empty folders, fixing incompatible file names, deleting files or directories based on patterns, moving files between locations, flattening directory structures, and renaming files or directories with pattern matching.
Practical examples include:
- Automatically removing empty folders from a project directory.
- Renaming files to remove characters that are not allowed on Windows systems.
- Deleting log files older than a certain date using regex patterns.
- Moving image files from multiple subfolders into a single target folder.
- Flattening nested directories by moving all files to the root directory.
- Renaming files according to a naming convention using regex replacements.
Properties
| Name | Meaning |
|---|---|
| Directory Path | The path of the directory where the operation will be performed. |
| Pattern | A regular expression pattern to match files or directories for filtering during the operation. |
| Recursive | Whether to process subdirectories recursively (true/false). |
| Include Types | Select which types of items to include: Files, Subdirectories, or both. |
| Use Shell Command | Use shell commands for faster file deletion (only on Linux/Unix systems). |
| Shell delete File Extensions | When using shell command deletion, select file extensions to delete (multiple selections allowed). |
| Shell Custom Extensions | Custom file extensions (comma-separated) to delete when using shell command deletion. |
| Delete Root Directory | Delete the entire directory tree including the root directory itself (use with caution). |
| Stage Test | Simulate the operation without actually performing it (dry run). |
These properties apply specifically to the Delete Files operation:
- Directory Path (required): Target directory to delete files from.
- Pattern (optional): Regex pattern to filter files/directories to delete.
- Recursive (default true): Whether to include subdirectories.
- Include Types (default ["files"]): Choose whether to delete files, subdirectories, or both.
- Use Shell Command (default false): If enabled, uses shell commands for deletion.
- Shell delete File Extensions: List of predefined file extensions to delete when using shell.
- Shell Custom Extensions: Additional custom file extensions to delete when using shell.
- Delete Root Directory (default false): Whether to delete the root directory itself.
- Stage Test (default true): Run in simulation mode without actual deletion.
Output
The output is a JSON array where each item corresponds to an input item processed. For the Delete Files operation, the output includes a result field containing details about the deletion operation, such as which files or directories were deleted or matched.
Example output structure snippet for deleteFiles operation:
{
"result": {
"deletedFiles": ["file1.txt", "file2.log"],
"deletedDirectories": ["emptyFolder"],
"skippedFiles": ["keepme.doc"],
"success": true
}
}
If the operation is run in stage test mode, no actual deletion occurs but the result simulates what would have been deleted.
No binary data output is produced by this node.
Dependencies
- The node relies on internal helper functions imported from local modules to perform file system operations.
- For shell command deletion, it requires a Linux/Unix environment with appropriate shell access.
- No external API keys or credentials are needed.
- The node expects valid filesystem paths accessible by the n8n runtime environment.
Troubleshooting
Common issues:
- Invalid directory paths or insufficient permissions can cause errors.
- Using shell command deletion on unsupported platforms (e.g., Windows) will fail.
- Incorrect regex patterns may result in no files being matched or unintended files being affected.
- Enabling "Delete Root Directory" deletes the entire directory tree, which can lead to data loss if used unintentionally.
Error messages:
- Errors related to file access or permission denied indicate the node cannot read/write/delete files at the specified location. Check user permissions and path correctness.
- Regex syntax errors mean the provided pattern is invalid; verify the regex pattern.
- Shell command failures usually indicate platform incompatibility or missing shell utilities.
Resolutions:
- Verify directory paths exist and are accessible.
- Test regex patterns separately before use.
- Avoid enabling shell command deletion on non-Unix systems.
- Use "Stage Test" mode to simulate operations safely before actual execution.
Links and References
- Node.js fs module documentation
- Regular Expressions (MDN)
- Linux rm command manual (relevant for shell deletion)
- n8n Documentation