Cant delete file from SP online document library using Powershell.

Occasional Contributor

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.



Eswar T






5 Replies

@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. 

@Beau Cameron ,


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")

Write-host -f Magenta $fname



Name is the name of the column value in the document library , I am checking with and using delete to delete the file.




@Eswar2015 So, this is a bit difficult to follow, but I think you need to use DeleteObject(). Take a look at this blog post


It's using ID, but you can change that for matching against your title.

@Beau Cameron ,


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.



Eswar T





EncodedAbsUrl property should give you the full URL including file name i.e. item["EncodedAbsUrl"]