PxPlus User Forum

Twitter Twitter Twitter

Author Topic: How to remove a file from an FTP server using Curl  (Read 3883 times)

Mike Hatfield

  • Gold Member
  • ****
  • Posts: 70
    • View Profile
How to remove a file from an FTP server using Curl
« on: June 05, 2019, 08:34:11 PM »
Hi All,

We have written our code to access a Magento FTP Server using CURL to download orders from a folder in json format.
To avoid processing the same orders I need to remove the file from the Magento FTP server as we process it.
That remove command structure is eluding me.
Has anyone removed a file from an FTP Server?
Thanks
Mike H

Devon Austen

  • Administrator
  • Diamond Member
  • *****
  • Posts: 382
  • Don’t Panic
    • View Profile
    • PVX Plus Technologies
Re: How to remove a file from an FTP server using Curl
« Reply #1 on: June 06, 2019, 09:11:44 AM »
cURL doesn't support FTP delete directly but it does allow you to execute FTP commands manually. To delete you can send a FTP delete command manually using cuRL.

-Q -DELE /path/to/filetodelete
Principal Software Engineer for PVX Plus Technologies LTD.

Mike Hatfield

  • Gold Member
  • ****
  • Posts: 70
    • View Profile
Re: How to remove a file from an FTP server using Curl
« Reply #2 on: June 07, 2019, 12:14:58 AM »
Hi Devon,

I tried your suggestion

F:\HIT\TEMP\FTPFILES>curl -k --ssl -Q -DELE ftp://ftp.servername.com.au/public_html/Future/Texts/filename.txt --user username@servername.com.au:password

This is what came back

052C10398880632B4914768747600603C351BF179135309830EE638478376AFA
curl: (21) QUOT string not accepted: DELE


21 FTP quote error. A quote command returned error from the server.
Mike H

Devon Austen

  • Administrator
  • Diamond Member
  • *****
  • Posts: 382
  • Don’t Panic
    • View Profile
    • PVX Plus Technologies
Re: How to remove a file from an FTP server using Curl
« Reply #3 on: June 07, 2019, 08:04:52 AM »
I missed the quotes and the URL and the delete command are separate. Try this:

curl -k -ssl --user username@servername.com.au:password ftp://ftp.servername.com.au/public_html/Future/Texts/ -Q "-DELE filename.txt"
Principal Software Engineer for PVX Plus Technologies LTD.

Mike Hatfield

  • Gold Member
  • ****
  • Posts: 70
    • View Profile
Re: How to remove a file from an FTP server using Curl
« Reply #4 on: June 07, 2019, 08:19:04 PM »
Hi Devon,

I thought it was going to work but sadly not....
The CURL command seems to be accepted but it just hangs and eventually it times out

curl -k --ssl --user username@servername.com.au:password ftp://ftp.servername.com.au/public_html/Future/Texts/ -Q " -DELE filename.txt"

curl: (28) Timeout was reached

I don't think its the directory (0755) file permissions (0744) because curl didn't complain
Mike H

Devon Austen

  • Administrator
  • Diamond Member
  • *****
  • Posts: 382
  • Don’t Panic
    • View Profile
    • PVX Plus Technologies
Re: How to remove a file from an FTP server using Curl
« Reply #5 on: June 10, 2019, 08:13:44 AM »
Our FTP utility uses a command like that to do deletes so as far as I known it should work.

Maybe try getting rid of the space before the -DELE
Maybe try moving the path from the server URL to in front of the filename

If those suggestions don't work you may want to try curl support as this is now strictly a curl question.
Principal Software Engineer for PVX Plus Technologies LTD.

Mike Hatfield

  • Gold Member
  • ****
  • Posts: 70
    • View Profile
Re: How to remove a file from an FTP server using Curl
« Reply #6 on: June 10, 2019, 11:05:40 AM »
Removing the space certainly made a difference.
It now produces a directory listing on the command window but doesn't delete anything.
That's because the ftp url ends with /Texts/ which generates a listing.
So it seems the -Q onwards was ignored.
As you say, time to consult with curl support.
Thanks for trying.
Mike H

Mike Hatfield

  • Gold Member
  • ****
  • Posts: 70
    • View Profile
Re: How to remove a file from an FTP server using Curl
« Reply #7 on: June 11, 2019, 04:51:22 AM »
This worked!
-O or -o is needed depending on whether you want to download to other than the current folder

curl -k -ssl --user username@servername.com.au:password -o \hit\somefolder\Magento\Orders\filename.txt ftp://ftp.servername.com.au/public_html/Future/Texts/ -Q "-DELE filename.txt"

I had to be patient though.
It appeared the file wasn't deleting whilst monitoring the ftp server with Filezilla.
It just takes a while to refresh.

Mike H

Peter.Higgins

  • Diamond Member
  • *****
  • Posts: 124
    • View Profile
Re: How to remove a file from an FTP server using Curl
« Reply #8 on: June 20, 2019, 04:27:29 PM »
Mike,
I recently ran into a hosted ftp curl delete issue and the answer was to put a ./ before the filepath.