How to delete a file using PHP ? Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report To delete a file by using PHP is very easy. Deleting a file means completely erase a file from a directory so that the file is no longer exist. PHP has an unlink() function that allows to delete a file. The PHP unlink() function takes two parameters $filename and $context. Syntax: unlink( $filename, $context ); Below programs illustrate the above approach: Program 1: This program uses unlink() function to remove file from directory. Suppose there is a file named as "gfg.txt" php <?php // PHP program to delete a file named gfg.txt // using unlink() function $file_pointer = "gfg.txt"; // Use unlink() function to delete a file if (!unlink($file_pointer)) { echo ("$file_pointer cannot be deleted due to an error"); } else { echo ("$file_pointer has been deleted"); } ?> Output: gfg.txt has been deleted Program 2: This program uses unlink() function to delete a file from folder after using some operation. php <?php // PHP program to delete a file named gfg.txt // using unlink() function $file_pointer = fopen('gfg.txt', 'w+'); // writing on a file named gfg.txt fwrite($file_pointer, 'A computer science portal for geeks!'); fclose($file_pointer); // Use unlink() function to delete a file if (!unlink($file_pointer)) { echo ("$file_pointer cannot be deleted due to an error"); } else { echo ("$file_pointer has been deleted"); } ?> Output: Warning: unlink() expects parameter 1 to be a valid path, resource given in C:\xampp\htdocs\server.php on line 12 Resource id #3 cannot be deleted due to an error Note: If the file does not exist then it will display an error. PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. Comment More infoAdvertise with us Next Article Interview Preparation For Software Developers N NishanthReddy7 Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like