Delete empty folders in a SFTP

Post Reply
Aristide
Newbie
Posts: 18
Joined: Tue Feb 11, 2025 3:05 pm

Delete empty folders in a SFTP

Post by Aristide »

Hello,

I try to create a simple flow dedicated to delete empty folders every hours in a SFTP server directory.
I don't find a solution to setup this condition...

Does someone has an idea ?

Thank you

AF
User avatar
magnussandstrom
Advanced member
Posts: 559
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Delete empty folders in a SFTP

Post by magnussandstrom »

You should be able to use FTP recieve for this:
Server type: SFTP
Leave originals on server: No
Check every (minutes): 60
Aristide
Newbie
Posts: 18
Joined: Tue Feb 11, 2025 3:05 pm

Re: Delete empty folders in a SFTP

Post by Aristide »

Hello Magnus,
thank you for your answer.

But I don't want to touch the folders that contain files, or the files in the root directory...

I don't see how to do it...
User avatar
magnussandstrom
Advanced member
Posts: 559
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Delete empty folders in a SFTP

Post by magnussandstrom »

Oh sorry I misread your original question. I think you need the scripting module to be able to create that feature within a Switch flow. Do you have that?
Aristide
Newbie
Posts: 18
Joined: Tue Feb 11, 2025 3:05 pm

Re: Delete empty folders in a SFTP

Post by Aristide »

Thank you Magnus, no problem !
Yes, I also thought it couldn't be that easy...

I have the scripting module, but I lack knowledge in development.
I have no idea how I could write this script...
jan_suhr
Advanced member
Posts: 702
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Delete empty folders in a SFTP

Post by jan_suhr »

I think you should consider other options for customer file upload that also are a lot more safe.

There are a few connectors for such services on the App store, for example Smash. Look in the category "Communication"

It will need a web portal interface instead of FPT-clients but it will give you a more modern and safer way to receive files and you don't need the FTP anymore.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
User avatar
magnussandstrom
Advanced member
Posts: 559
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Delete empty folders in a SFTP

Post by magnussandstrom »

jan_suhr wrote: Wed Mar 04, 2026 3:38 pm I think you should consider other options for customer file upload that also are a lot more safe.

There are a few connectors for such services on the App store, for example Smash. Look in the category "Communication"

It will need a web portal interface instead of FPT-clients but it will give you a more modern and safer way to receive files and you don't need the FTP anymore.
To OP’s defense, there is nothing inherently wrong with SFTP from a security perspective, it’s quite different from plain FTP and is generally considered secure when configured properly.

That said, if we understood the full workflow and requirements, there might be alternative approaches that simplify the process and avoid the need for regularly cleaning up empty folders.
User avatar
magnussandstrom
Advanced member
Posts: 559
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Delete empty folders in a SFTP

Post by magnussandstrom »

Aristide wrote: Wed Mar 04, 2026 3:11 pm Thank you Magnus, no problem !
Yes, I also thought it couldn't be that easy...

I have the scripting module, but I lack knowledge in development.
I have no idea how I could write this script...
Are you running the Switch server on Windows or Mac?
jan_suhr
Advanced member
Posts: 702
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Delete empty folders in a SFTP

Post by jan_suhr »

magnussandstrom wrote: Thu Mar 05, 2026 8:12 am To OP’s defense, there is nothing inherently wrong with SFTP from a security perspective, it’s quite different from plain FTP and is generally considered secure when configured properly.

That said, if we understood the full workflow and requirements, there might be alternative approaches that simplify the process and avoid the need for regularly cleaning up empty folders.
One very big obstacle with FTP is that you don't get much Metadata from an FTP-upload. Maybe and order number if the uploader added that to the filename.

With a web portal you can get a lot of information for the job and making the uploaded file process through Switch all the way to the production unit.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
User avatar
magnussandstrom
Advanced member
Posts: 559
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Delete empty folders in a SFTP

Post by magnussandstrom »

I built a small program (Windows only) to use in a Switch flow that you can try out if you want (at your own risk of course):

Download here: https://tinyurl.com/3jwyjpe8

Just edit these variables in the Run command-app and you are ready to go:

Code: Select all

"C:\yourpath\cleanup-sftp.exe" --host sftp.server.com --user username --password "password" --root "/" --min-age-minutes 1
Info:

The --min-age-minutes parameter is for preventing the deletion of folders that just have been created. But if mtime is not provided by the SFTP server you need to set it to 0 for it to work.

--root "/" means the root for the specific user, if you want you can set it to another directory like --root "/subfolder1/myfolder"

To run the check every 60 min I would suggest to add the Switch app FireStarter in the beginning of the flow and set it to 60 min.

The log folder is just a suggestion and can of course be deleted if you want to.

It works good for me, but please try on a test root folder first to ensure that the behaviour is what you are looking for.


cleanupsftp.png
cleanupsftp.png (39.29 KiB) Viewed 146 times
Post Reply