Adobe Campaign - Delete a file from SFTP

Adobe Campaign - Delete a file from SFTP


Navigate:
 
Sometimes you may want to delete a file from the SFTP environment, because it didn’t need to exist for long and will just clutter things up. Sure, you could go and delete it manually, but this is an automation platform and we should automate it because there are only so many hours in the day.
 
notion image
 
For example, I’m creating a report here - extracting it to the SFTP, attaching it to an email that goes out - but then afterwards I’d like to delete the report from SFTP because it’s not really useful anymore.
 
After a file extraction activity is completed, an event variable is created for the target path that it’s on. (Remember, event variables only exist on that path, they’re not accessible anywhere else)
notion image
For example, in this screenshot - an event variable that was created on the BLUE path would not be available on the RED path.
 
The file extraction activity sets a variable called vars.filename in the background - which will contain the exact path to the file on the SFTP server as a string. For example, this might be something like: “/sftp/incoming/TestFolder/testexport.csv”
 
We’ll be using that variable, along with functions on the File object, which are listed in the Adobe API documentation here.
 
The two we’ll be using are:
  • new File()
  • remove()
 
In a javascript code activity, use the following snippet:
var directory = new File(vars.filename); directory.remove();
 
As you can see in the javascript code, we create a JS variable called directory, which reads the file which is located at vars.filename
 
We then run the remove function on that file.
 
And….that is literally it. File is now deleted as soon as the workflow is run and it reaches the activity. Bit of a weird feeling for sure, no warning - just deletion, but give it a few test runs and you'll feel way more comfortable.