Forum Discussion
Cant delete file from SP online document library using Powershell.
Hi All,
I have the following requirement delete a file from SP Document library fom all folders & Subfolders.
Here is my script (delete file.txt).
Want to delete a file by name,
Kindly suggest where, I am going wrong.
Thanks,
Eswar T
Thanks,
Eswar
Eswar2015 I'm having a hard time seeing in your script where your delete is happening. But based on the error, you are getting the document title and trying to delete that. You need to get a reference to the file object using the Title, and delete the file object.
- Eswar2015Copper Contributor
Greetings , this is what i am trying based on title of the column in this case : Name I am trying to delete the file.
The script runs with no errors , but i dont get the file deleted.
Please find the below lines of code , I made changes to & kindly suggest.
if($fname["Name"] -eq "notfoundchanged5.34.xslx")
{
$Ctx.load($Folder.files)
$fname.delete()Write-host -f Magenta $fname
$Ctx.ExecuteQuery()}
Name is the name of the column value in the document library , I am checking with and using delete to delete the file.
Regards,
Eswar.
Eswar2015 So, this is a bit difficult to follow, but I think you need to use DeleteObject(). Take a look at this blog post http://www.sharepointdiary.com/2017/02/sharepoint-online-delete-file-from-document-library-using-powershell.html
It's using ID, but you can change that for matching against your title.