FluentFTP icon indicating copy to clipboard operation
FluentFTP copied to clipboard

Is it possible to delete all files from a directory in one command? (empty a directory)

Open wertzui opened this issue 3 years ago • 5 comments

FTP OS: Unix / Windows

FTP Server: Multiple and unknown

Computer OS: Linux

FluentFTP Version: 40.0.0

Framework: .NET 6

I need to delete all files in a directory on the server, but not the directory itself. Is that possible with one command, or do I first need to call GetListing() and then DeleteFile() for all files?

wertzui avatar Sep 14 '22 09:09 wertzui

Can you try DeleteDirectory and then CreateDirectory.

DeleteDirectory has in-built recursion.

robinrodricks avatar Sep 15 '22 09:09 robinrodricks

That would also delete the folder. I need to keep the folder, because often the folder (or one file in it) is in use by another application. Because of that I need some kind of "best effort" attempt to delete all files in one folder.

wertzui avatar Sep 20 '22 07:09 wertzui

Many clients have an "MDEL", "MPUT" and "MGET" client command (with or without prompting) and they internally do the needed GetListing and globbing to allow such operations as

MDEL *.*

I did a Wireshark trace and I could see they typically use NLST and then loop through the names, but that is client and server dependant.

If there were a better way, these clients surely would do it differently. The servers simply do not have this command, which is sad.

So I propose an addition to the bucket list as an enhancement: "Implement MDEL, MPUT and MGET type of operations".

FanDjango avatar Sep 20 '22 09:09 FanDjango

Ok, I did some more research. Some servers have actually got specialized RMDIR commands, like for example proftpd. And these are known to FluentFTP -

Here is the current list of such servers: Serv-U, proftpd. How impressive.

In all other cases, FluentFTPs DeleteDirectory(...) will use its own GetListing(...)...delete-loop.

So, to make this work for you, all we need to do is make an option to DeleteDirectory(...) to leave the final directory intact.

FanDjango avatar Sep 20 '22 13:09 FanDjango

..which I have done. There is PR out there now and once it is merged, you can do this:

New function entirely: EmptyDirectory(...) i.e. "make a directory become empty".

Using this will prevent use of any highly optimised server side RMDIR functionality on the uppermost level, but that's the price.

Usage:

	/// <summary>
	/// Deletes the specified directory and all its contents.
	/// </summary>
	/// <param name="path">The full or relative path of the directory to delete</param>
	/// <param name="options">Useful to delete hidden files or dot-files.</param>
	public void EmptyDirectory(string path, FtpListOption options)

FanDjango avatar Sep 20 '22 14:09 FanDjango

@wertzui See the above amended usage for EmptyDirectory(...). It has been merged and you could use it from now by getting the master branch down from GitHub.

Since some other things also cooking right now, a Nuget release is not immediately scheduled.

FanDjango avatar Sep 22 '22 17:09 FanDjango