How to Delete a OneDrive File by URL Using the OneDrive API
Question details
Developers are looking for a way to delete a file in OneDrive using its URL rather than the standard Microsoft Graph or OneDrive file ID.

- Product
- OneDrive API / Microsoft Graph
- Device & OS
- not provided
- Scenario
- A developer needs to programmatically delete a OneDrive file but only has access to the file's sharing URL or web URL.
- Observed behavior
- The API documentation primarily focuses on deletion via file ID, leaving developers unsure of the correct request format to delete directly via URL.
Ensure you have an active Microsoft Graph access token and that your application has been granted the necessary permissions (such as Files.ReadWrite.All) to modify or delete files.
Resolve the URL to a DriveItem using Microsoft Graph API
While you cannot delete a file directly using just the web URL in a single endpoint, you can use the Graph API to resolve the URL into a DriveItem and then delete it using the retrieved file ID.
The Microsoft Graph API allows you to encode a sharing URL and use the 'shares' endpoint to access the corresponding DriveItem. Once the item's unique ID is retrieved, standard deletion methods apply.
Base64 encode the OneDrive file URL. Replace any '/' with '_' and '+' with '-', then append 'u!' to the beginning to create a valid sharing token.
Make an authenticated GET request to 'https://graph.microsoft.com/v1.0/shares/{encoded-url}/driveItem'. Parse the JSON response to extract the 'id' field.
Send a DELETE request to 'https://graph.microsoft.com/v1.0/me/drive/items/{extracted-id}' (or the specific drive path) to permanently remove the file.

Consult the Microsoft Q&A Community for Official API Guidance
If you encounter specific errors or need guidance on edge cases related to OneDrive APIs, reaching out to Microsoft Graph experts is the best approach.
Try WPS Office for Seamless Document Management
While API development and complex cloud integrations require tools like Microsoft Graph, your daily document creation and editing needs can be handled perfectly by WPS Office. It is a lightweight, cost-effective alternative to Microsoft Office that easily manages your local and cloud documents.
- 1. Download the software: Visit the official WPS Office website to download the installer for your operating system.
- 2. Install and launch: Run the lightweight installer and open the application to access Writer, Spreadsheets, and Presentation modules.
- 3. Edit your documents: Open your existing Microsoft Office files and edit them seamlessly without worrying about formatting issues.

Frequently Asked Questions
Can I perform a DELETE request directly on a OneDrive web URL?
No, standard REST APIs for OneDrive (like Microsoft Graph) do not accept DELETE requests directly on web URLs. You must interact with the API endpoints using the item's unique ID.
What permissions do I need to delete a file via Microsoft Graph API?
Your application must be granted 'Files.ReadWrite' or 'Files.ReadWrite.All' permissions. The token used for the API call must reflect these granted scopes.
How do I test OneDrive API requests before deploying my code?
You can use the Microsoft Graph Explorer, a free web-based tool provided by Microsoft, to safely test GET and DELETE requests on your OneDrive files using your developer account.




