SQL Server guide

Delete a file or folder


Deletes a file or folder. Identify the item by ID or path in the WITH clause. This action cannot be undone.

Standard SQL query example

This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.

DELETE FROM Files
WITH (
     DriveId='me'
   --OR--
   --DriveId='b!GtLN726LE0eY5F2BBNi14'
   
   --Enter file path or ID 
   
   --,ItemId='root:/dump.pdf:'	--Path must end with colon
   --,ItemId='root:/myfolder/dump.pdf:'
   --,ItemId='01SUOJPKHXMPKD2UXXXXXXXXXXXXXXXXXX'
	)

--Using Table Name instead of endpoint (with Id / Path in WHERE clause for simple Syntax)

--************	
--Delete File by ID	or Path
--************
--DELETE From Files Where Id='01N3NI7YU6DYBSLCEDKBB23CR4FSWZYSDJ'
--DELETE From Files Where Id='root:/test_out.csv:'
--DELETE From Files Where Id='root:/somefolder/test_out.csv:'

--************
--Delete Folder by ID or Path
--************
--DELETE From Folders Where Id='01N3NI7YU6DYBSLCEDKBB23CR4FSWZYSDJ'
--DELETE From Folders Where Id='root:/somefolder:'
--DELETE From Folders Where Id='root:/somefolder/childfolder:'
	
--DriveId can be retrieved by selecting from 'list_drives' endpoint.
--FileId can be retrieved by selecting from 'list_folder' or 'list_root' endpoints.

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_ONEDRIVE_IN_GATEWAY], 'DELETE FROM Files
WITH (
     DriveId=''me''
   --OR--
   --DriveId=''b!GtLN726LE0eY5F2BBNi14''
   
   --Enter file path or ID 
   
   --,ItemId=''root:/dump.pdf:''	--Path must end with colon
   --,ItemId=''root:/myfolder/dump.pdf:''
   --,ItemId=''01SUOJPKHXMPKD2UXXXXXXXXXXXXXXXXXX''
	)

--Using Table Name instead of endpoint (with Id / Path in WHERE clause for simple Syntax)

--************	
--Delete File by ID	or Path
--************
--DELETE From Files Where Id=''01N3NI7YU6DYBSLCEDKBB23CR4FSWZYSDJ''
--DELETE From Files Where Id=''root:/test_out.csv:''
--DELETE From Files Where Id=''root:/somefolder/test_out.csv:''

--************
--Delete Folder by ID or Path
--************
--DELETE From Folders Where Id=''01N3NI7YU6DYBSLCEDKBB23CR4FSWZYSDJ''
--DELETE From Folders Where Id=''root:/somefolder:''
--DELETE From Folders Where Id=''root:/somefolder/childfolder:''
	
--DriveId can be retrieved by selecting from ''list_drives'' endpoint.
--FileId can be retrieved by selecting from ''list_folder'' or ''list_root'' endpoints.')

Using EXEC in SQL Server (handling larger SQL text)

The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements. This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).

Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_ONEDRIVE_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = 'DELETE FROM Files
WITH (
     DriveId=''me''
   --OR--
   --DriveId=''b!GtLN726LE0eY5F2BBNi14''
   
   --Enter file path or ID 
   
   --,ItemId=''root:/dump.pdf:''	--Path must end with colon
   --,ItemId=''root:/myfolder/dump.pdf:''
   --,ItemId=''01SUOJPKHXMPKD2UXXXXXXXXXXXXXXXXXX''
	)

--Using Table Name instead of endpoint (with Id / Path in WHERE clause for simple Syntax)

--************	
--Delete File by ID	or Path
--************
--DELETE From Files Where Id=''01N3NI7YU6DYBSLCEDKBB23CR4FSWZYSDJ''
--DELETE From Files Where Id=''root:/test_out.csv:''
--DELETE From Files Where Id=''root:/somefolder/test_out.csv:''

--************
--Delete Folder by ID or Path
--************
--DELETE From Folders Where Id=''01N3NI7YU6DYBSLCEDKBB23CR4FSWZYSDJ''
--DELETE From Folders Where Id=''root:/somefolder:''
--DELETE From Folders Where Id=''root:/somefolder/childfolder:''
	
--DriveId can be retrieved by selecting from ''list_drives'' endpoint.
--FileId can be retrieved by selecting from ''list_folder'' or ''list_root'' endpoints.'
EXEC (@MyQuery) AT [LS_TO_ONEDRIVE_IN_GATEWAY]