Forum Discussion
Cant delete file from SP online document library using Powershell.
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.
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.
- Beau CameronJul 10, 2019MVP
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.
- Eswar2015Jul 11, 2019Copper Contributor
Thanks for the reply,will check actually my file relative url is based on a name like (SOP & it should check all files in the library and should delete those files whose name starts with SOP).
Do we have any dynamic way, to get the fileref/url name either by PNP or by PS script.
Kindly Suggest.
Thanks,
Eswar T
- Toby StathamJul 19, 2019Brass Contributor
EncodedAbsUrl property should give you the full URL including file name i.e. item["EncodedAbsUrl"]