Cant delete file from SP online document library using Powershell.

%3CLINGO-SUB%20id%3D%22lingo-sub-744812%22%20slang%3D%22en-US%22%3ECant%20delete%20file%20from%20SP%20online%20document%20library%20using%20Powershell.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-744812%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20the%20following%20requirement%20delete%20a%20file%20from%20SP%20Document%20library%20fom%20all%20folders%20%26amp%3B%20Subfolders.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20my%20script%20(delete%20file.txt).%3C%2FP%3E%3CP%3EWant%20to%20delete%20a%20file%20by%20name%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EKindly%20suggest%20where%2C%20I%20am%20going%20wrong.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EEswar%20T%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EEswar%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-745040%22%20slang%3D%22en-US%22%3ERe%3A%20Cant%20delete%20file%20from%20SP%20online%20document%20library%20using%20Powershell.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-745040%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F372692%22%20target%3D%22_blank%22%3E%40Eswar2015%3C%2FA%3E%26nbsp%3BI'm%20having%20a%20hard%20time%20seeing%20in%20your%20script%20where%20your%20delete%20is%20happening.%20But%20based%20on%20the%20error%2C%20you%20are%20getting%20the%20document%20title%20and%20trying%20to%20delete%20that.%20You%20need%20to%20get%20a%20reference%20to%20the%20file%20object%20using%20the%20Title%2C%20and%20delete%20the%20file%20object.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-746694%22%20slang%3D%22en-US%22%3ERe%3A%20Cant%20delete%20file%20from%20SP%20online%20document%20library%20using%20Powershell.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-746694%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F39836%22%20target%3D%22_blank%22%3E%40Beau%20Cameron%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGreetings%2C%20its%20in%20this%20condition%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23list%20all%20files%20in%20Folder%3CBR%20%2F%3EForEach%20(%24File%20in%20%24Folder.files)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%23Get%20the%20File%20Name%20or%20do%20something%3CBR%20%2F%3EWrite-host%20-f%20Magenta%20%24File.Name%3CBR%20%2F%3E%24fname%20%3D%20%24File.Name%3CBR%20%2F%3Eif(%24fname%20%3D%20%22notfound.xslx%22)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24Ctx.load(%24Folder.files)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24fname.DeleteObject()%3C%2FP%3E%3CP%3EWrite-host%20-f%20Magenta%20%24fname%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Ctx.ExecuteQuery()%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20checking%20the%20name%20of%20the%20file%20and%20trying%20to%20delete%20the%20object%20using(DeleteObject)%2C%20kindly%20suggest.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-746725%22%20slang%3D%22en-US%22%3ERe%3A%20Cant%20delete%20file%20from%20SP%20online%20document%20library%20using%20Powershell.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-746725%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F39836%22%20target%3D%22_blank%22%3E%40Beau%20Cameron%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGreetings%20%2C%20this%20is%20what%20i%20am%20trying%20based%20on%20title%20of%20the%20column%20in%20this%20case%20%3A%20Name%20I%20am%20trying%20to%20delete%20the%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20script%20runs%20with%20no%20errors%20%2C%20but%20i%20dont%20get%20the%20file%20deleted.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20find%20the%20below%20lines%20of%20code%20%2C%20I%20made%20changes%20to%20%26amp%3B%20kindly%20suggest.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eif(%24fname%5B%22Name%22%5D%20-eq%20%22notfoundchanged5.34.xslx%22)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24Ctx.load(%24Folder.files)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24fname.delete()%3C%2FP%3E%3CP%3EWrite-host%20-f%20Magenta%20%24fname%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Ctx.ExecuteQuery()%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EName%20is%20the%20name%20of%20the%20column%20value%20in%20the%20document%20library%20%2C%20I%20am%20checking%20with%20and%20using%20delete%20to%20delete%20the%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%2C%3C%2FP%3E%3CP%3EEswar.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-747519%22%20slang%3D%22en-US%22%3ERe%3A%20Cant%20delete%20file%20from%20SP%20online%20document%20library%20using%20Powershell.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-747519%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F372692%22%20target%3D%22_blank%22%3E%40Eswar2015%3C%2FA%3E%26nbsp%3BSo%2C%20this%20is%20a%20bit%20difficult%20to%20follow%2C%20but%20I%20think%20you%20need%20to%20use%20DeleteObject().%20Take%20a%20look%20at%20this%20blog%20post%26nbsp%3B%3CA%20href%3D%22http%3A%2F%2Fwww.sharepointdiary.com%2F2017%2F02%2Fsharepoint-online-delete-file-from-document-library-using-powershell.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.sharepointdiary.com%2F2017%2F02%2Fsharepoint-online-delete-file-from-document-library-using-powershell.html%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt's%20using%20ID%2C%20but%20you%20can%20change%20that%20for%20matching%20against%20your%20title.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-750114%22%20slang%3D%22en-US%22%3ERe%3A%20Cant%20delete%20file%20from%20SP%20online%20document%20library%20using%20Powershell.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-750114%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F39836%22%20target%3D%22_blank%22%3E%40Beau%20Cameron%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20the%20reply%2Cwill%20check%20actually%20my%20file%20relative%20url%20is%20based%20on%20a%20name%20like%20(SOP%20%26amp%3B%20it%20should%20check%20all%20files%20in%20the%20library%20and%20should%20delete%20those%20files%20whose%20name%20starts%20with%20SOP).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20we%20have%20any%20dynamic%20way%2C%20to%20get%20the%20fileref%2Furl%20name%20either%20by%20PNP%20or%20by%20PS%20script.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EKindly%20Suggest.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EEswar%20T%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-764096%22%20slang%3D%22en-US%22%3ERe%3A%20Cant%20delete%20file%20from%20SP%20online%20document%20library%20using%20Powershell.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-764096%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F372692%22%20target%3D%22_blank%22%3E%40Eswar2015%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EEncodedAbsUrl%26nbsp%3B%3C%2FSTRONG%3Eproperty%20should%20give%20you%20the%20full%20URL%20including%20file%20name%20i.e.%20item%5B%22EncodedAbsUrl%22%5D%3C%2FP%3E%3C%2FLINGO-BODY%3E
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.

 

Thanks,

Eswar T

 

 

 

Thanks,

Eswar

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")
{
$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-pow...

 

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.

 

Thanks,

Eswar T

 

 

@Eswar2015 

 

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