Forum Discussion
Eswar2015
Jul 09, 2019Copper Contributor
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 ...
Eswar2015
Jul 10, 2019Copper 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.
Beau Cameron
Jul 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, 2019Copper Contributor
EncodedAbsUrl property should give you the full URL including file name i.e. item["EncodedAbsUrl"]