How to delete a file using Application.dataPath ?

I have the following code in the Update() function:

		if(Input.GetKeyDown(KeyCode.F9))
		{
			File.Delete (Application.dataPath + "/Ob4.txt");
		}

I want to be able to delete the saved file whenever I press a button.
But whenever I press F9 the file still remains in the Assets folder.

You need to tab out of Unity then tab back in for the project folder to refresh. Or refresh the asset database in code:

// refresh editor view
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif

Also, simply just check the file location with your file explorer to see if the file has been added/removed.


Edit : here is an example script. Create a new scene, attach script to an empty gameObject, and hit play. Editor project window refreshes when a file is created or deleted.

CreateAndDeleteFile.cs

using UnityEngine;
using System.Collections;
using System.IO;

public class CreateAndDeleteFile : MonoBehaviour 
{
	public string fileName = "ob04";
	public string fileExtension = ".txt";
	
	private string guiMessage = "type in a file name and hit save";
	
	
	void OnGUI() 
	{
		GUI.Box( new Rect( 160, 20, 400, 25 ), guiMessage );
		
		fileName = GUI.TextField( new Rect( 20, 20, 120, 25 ), fileName );
		
		if ( GUI.Button( new Rect ( 20, 60, 100, 30 ), "LOAD" ) )
		{
			LoadFile();
		}
		
		if ( GUI.Button( new Rect ( 20, 100, 100, 30 ), "SAVE" ) )
		{
			SaveFile();
		}
		
		if ( GUI.Button( new Rect ( 20, 140, 100, 30 ), "DELETE" ) )
		{
			DeleteFile();
		}
	}
	
	
	void LoadFile() 
	{
		string filePath = Application.dataPath + "/" + fileName + fileExtension;
		
		// check if file exists
		if ( !File.Exists( filePath ) )
		{
			guiMessage = "no " + fileName + " file exists"; //Debug.Log( "no " + fileName + " file exists" );
		}
		else
		{
			guiMessage = fileName + " file exists, reading..."; //Debug.Log( fileName + " file exists, reading ..." );
			
			// read file
			StreamReader readFile = new StreamReader( filePath );
			string readString = readFile.ReadToEnd(); //Debug.Log( readFile.ReadToEnd() );
			readFile.Close();
			
			guiMessage = guiMessage + " : " + readString;
		}
	}
	
	
	void SaveFile() 
	{
		string filePath = Application.dataPath + "/" + fileName + fileExtension;
		
		// check if file exists
		if ( !File.Exists( filePath ) )
		{
			guiMessage = "no " + fileName + " file exists, creating..."; //Debug.Log( "no " + fileName + " file exists, creating..." );
			
			// create new file
			StreamWriter newFile = new StreamWriter( filePath );
			newFile.WriteLine( "Hello, I am a text file :)" );
			newFile.Close();
			
			RefreshEditorProjectWindow();
		}
		else
		{
			guiMessage = fileName + " file already exists"; //Debug.Log( fileName + " file already exists" );
		}
	}
	
	
	void DeleteFile() 
	{
		string filePath = Application.dataPath + "/" + fileName + fileExtension;
		
		// check if file exists
		if ( !File.Exists( filePath ) )
		{
			guiMessage = "no " + fileName + " file exists"; //Debug.Log( "no " + fileName + " file exists" );
		}
		else
		{
			guiMessage = fileName + " file exists, deleting..."; //Debug.Log( fileName + " file exists, deleting..." );
			
			File.Delete( filePath );
			
			RefreshEditorProjectWindow();
		}
	}
	
	
	void RefreshEditorProjectWindow() 
	{
		#if UNITY_EDITOR
		UnityEditor.AssetDatabase.Refresh();
		#endif
	}
}