CURL command line tool - Delete File from FTP server
Asked Answered
G

4

11

I'm actually trying to use CURL to make some operations on a ftp server in C++ with Visual Studio. I've no trouble to do some uploads or downloads with commande line tools.

But for deleting some file I have some errors.

Here is the command I type:

curl -v -u username:pwd ftp://host/FileTodelete.xml -Q '-DELE FileTodelete.xml'

This is the answer:

* Adding handle: conn: 0x1ca5260
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x1ca5260) send_pipe: 1, recv_pipe: 0
* About to connect() to host port 21 (
*   Trying ......
* Connected to host (...) po
< 220-FileZilla Server version 0.9.49 beta
< 220 Bienvenue sur le serveur FTP de HandTrainer
> USER username
< 331 Password required for username
> PASS pwd
< 230 Logged on
> PWD
< 257 "/" is current directory.
* Entry path is '/'
> '-DELE
* ftp_perform ends with SECONDARY: 0
< 500 Syntax error, command unrecognized.
* QUOT command failed with 500
* Closing connection 0
curl: (21) QUOT command failed with 500
* Rebuilt URL to: FileTodelete.xml'/
* Adding handle: conn: 0x1ca5260
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 1 (0x1ca5260) send_pipe: 1, recv_pipe: 0
* Could not resolve host: FileTodelete.xml'
* Closing connection 1
curl: (6) Could not resolve host: FileTodelete.xml'

Moreover, the file is on the server so I don't understand.

Getraer answered 25/2, 2015 at 14:25 Comment(2)
Note that, if you just want to delete your file, probably the first bug is that the file should not be mentioned in the ftp:// URL. Otherwise, also with the correct deletion parameters, then you risk to delete the file but returning the exit error status code 78, since after deletion it automatically tries to access the deleted file (running SIZE FileTodelete.xml and obtaining a 550 Can't check for file existence and then - running RETR FileTodelete and obtaining 550 Can't open FileTodelete.xml: No such file or directory - as far as I've seen using --verbose). What was your source?Exegetics
I totally understand your comment and it seems right indeed. What do you mean by "source" ? I was on my Windows personal computer if I remember.Getraer
G
20

Problem solved! The dash before DELE should not be there:

curl -v -u username:pwd ftp://host/FileTodelete.xml -Q "DELE FileTodelete.xml"
Getraer answered 26/2, 2015 at 11:8 Comment(5)
What dash? IMHO @chaos's answer is just thisIota
@Iota I don't think that this answer mentions the user chaos at all. It just says that the original question (that comes from another user as far as I see) says -Q '-DELE FileTodelete.xml' with the dashExegetics
I think the answer is partially bugged. The correct command should be: curl -v -u username:pwd ftp://host/ -Q "DELE FileTodelete.xml" So without repeating the file in the URL. Otherwise, curl deletes the file but returning the exit error status code 78, since after deletion it automatically tries to access the deleted file (running SIZE FileTodelete.xml and obtaining a 550 Can't check for file existence and then - running RETR FileTodelete and obtaining 550 Can't open FileTodelete.xml: No such file or directory - as far as I've seen using --verbose)Exegetics
If you write a proper answer I will check it as an answer. I'm not working with that anymore for more than 9 years so I'm not sure I will and could check it. Dunno what to do in that cases. At the time it worked for me.Getraer
I can modify my answer too or maybe you can edit it I don't knowGetraer
C
9

You place a command with -Q, but -DELE file is not a common ftp command. Try one of these instead:

curl -v -u username:pwd ftp://host/FileTodelete.xml -Q 'DELE FileTodelete.xml'
curl -v -u username:pwd ftp://host/FileTodelete.xml -Q 'DELETE FileTodelete.xml'
curl -v -u username:pwd ftp://host/FileTodelete.xml -Q 'rm FileTodelete.xml'
Charland answered 25/2, 2015 at 14:43 Comment(5)
This doesn't work : / I tried the three but i got the same error message than previously.Getraer
I found what didn't work : trouble of permission for this user ! I'll post which command you submitted worked tonight.Getraer
Even after giving permission to the user, i still have the same error : 500 Syntax error, command unrecognized.Getraer
From the curl man page: To make commands take place after a successful transfer, prefix them with a dash '-'. To make commands get sent after libcurl has changed working directory, just before the transfer command(s), prefix the command with '+'.Habergeon
I think the answer is partially bugged. The correct command should be: curl -v -u username:pwd ftp://host/ -Q 'DELE FileTodelete.xml' (or DELETE or rm etc.) So without repeating the file in the URL. Otherwise, curl deletes the file but returning the exit error status code 78, since after deletion it automatically tries to access the deleted file (running SIZE FileTodelete.xml and obtaining a 550 Can't check for file existence and then - running RETR FileTodelete and obtaining 550 Can't open FileTodelete.xml: No such file or directory - as far as I've seen using --verbose)Exegetics
S
3

I accomplished this task by first logging into my FTP server then typing "?" at the command line to get a list of commands recognized by my FTP server. The command recognized by my server was "delete".

So, -Q"delete $fileToRemove" $serverURL

I was also able to get it to work by using -X"DELE $fileToRemove" $serverURL. However, I kept getting rc=19 (because I think the "-X" option mostly applies to HTTP|HTTPS?) from curl when I used this argument even though the file was successfully deleted.

Not sure if other FTP servers recognize different commands but this is what worked for me.

Saran answered 15/9, 2016 at 16:47 Comment(1)
I think running cURL in verbose mode, and uderstanding the underlying FTP commands with the ? is a good troubleshooting approach. Please just add more syntax highlight in this answer, so it's more cute. stackoverflow.com/editing-help#syntax-highlighting Also, I like this answer since it's the only one who has a clean server URL, instead of the others that has an incorrect server URL that includes the file to be deleted, so this answer does not cause a cURL exit error status code 78 and this is good.Exegetics
S
0

Additional info, to delete a file from non-root directory: stays on root and delete file with path on the -Q command only

curl ftp://192.168.140.XXXX:21 -Q 'DELE ISOS/foo.iso'
curl ftp://192.168.140.XXXX:21 -Q 'DELETE ISOS/foo.iso'
curl ftp://192.168.140.XXXX:21 -Q 'rm ISOS/foo.iso'
Sothena answered 18/5, 2023 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.