How do I delete the actual file by using ms-sql?

Hello,
I am just curious, is there anyway that I can delete actual file in the server by using ms-sql?
You know in ASP coding, I can delete, write ,or rename the file by using Server.CreateObject("Scripting.FileSystemObject").

Thank you.
erin027Asked:
Who is Participating?
 
YveauCommented:
before you can run a xp_cmdshell, you might need to run this to activate the feature:

sp_configure 'show advanced options', 1
go
reconfigure
go
sp_configure 'xp_cmdshell', 1
go
reconfigure
go

then run the statement that aneeshattingal posted:
EXEC master..xp_cmdShell " del <fileName> "

... and to do it real nice, disable the option again, right after you ran the command by:

sp_configure 'xp_cmdshell', 0
go
reconfigure
go
sp_configure 'show advanced options', 0
go
reconfigure
go

Leaving the xp_cmdshell feature available is a huge security risk !

Hope this helps ...
0
 
cjohnsen2Commented:
Sure.  Try this.

xp_cmdshell "cmd /c del <filename>"
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
run the following query

EXEC master..xp_cmdShell " del <fileName> " 
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
erin027Author Commented:
Yveau,
Thank you for telling me about the security risk too, but I've tried it and it seems like everything went through well because I do not see any error message, but when I check the file, it's still there.

The Result output came out like this:
The syntax of the command is incorrect.
NULL

Can you tell me what I've done wrong?

This were the coding:
sp_configure 'show advanced options', 1
go
reconfigure
go
sp_configure 'xp_cmdshell', 1
go
reconfigure
go
EXEC master..xp_cmdShell " del <D:\Sites\test\product_images> "
go
sp_configure 'xp_cmdshell', 0
go
reconfigure
go
sp_configure 'show advanced options', 0
go
reconfigure
go


I've checked the directory and it was all correct.
0
 
erin027Author Commented:
Yveau,
Nevermind. I got it. I shouldn't type the "<" ">" sign.
Thank you.
0
 
YveauCommented:
Glad I could be of any help !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.