title: Delete multiple files from folder using T-SQL without using cursor
body: <p>I am writing a cleanup script. This script will run on weekend and clean up the db. Tables are related to Eamils and path of attachments are being stored in table. In cleanup of tables I also have to delete files from folder.</p>
  
  <p>The path of files is like following.</p>
  
  <pre><code>\\xxx.xxx.xxx.xxx\EmailAttachments\Some Confirmation for xyz Children Centre_9FW4ZE1C57324B70EC79WZ15FT9FA19E.pdf
  </code></pre>
  
  <p>I can delete multiple files like following.</p>
  
  <pre><code>xp_cmdshell 'del c:\xyz.txt, abc.txt'
  </code></pre>
  
  <p>BUT when I create a CSV from table using FOR XML PATH('') the string cut off at the end. There might be 1000s of rows to delete so I don't want to use cursor to delete files from folder.</p>
  
  <ol>
  <li>How can I delete files from folder
  without using cursor</li>
  <li>What permissions do I need on
  network folder to delete files using t-sql from sql server</li>
  </ol>
  
  <p><strong>EDIT:</strong>
  I have used cursor and it looks ok, not taking so much time. One problem which I am facing is</p>
  
  <ul>
  <li><p>The sql server consider file name with space as two files like following statement </p>
  
  <p>xp_cmdshell 'del E:\Standard Invite.doc'</p></li>
  </ul>
  
  <p>throws error</p>
  
  <pre><code>Could Not Find E:\Standard
  Could Not Find C:\Windows\system32\Invite.doc
  NULL
  </code></pre>
  
  <p>Thanks.</p>