How to delete Save Files while deleting game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sirAlexDev

I created some .dat and .json files in the user:// folder . But it wasn’t deleted while deleting the game. How to make deleting all the created files during the game while deleting game possible

:bust_in_silhouette: Reply From: deaton64

Hi,
Not quite sure what you mean by it wasn’t deleted while deleting the game but here’s some code that will create 10 .dat & .json files and then delete all files by extension:

extends Node2D

func _ready() -> void:
	var save_game 
	for i in range(10):
		save_game = File.new()
		save_game.open("user://savegame"+str(i)+".json", File.WRITE)
		save_game.store_line("This is a test")
		save_game.close()

	for i in range(10):
		save_game = File.new()
		save_game.open("user://savegame"+str(i)+".dat", File.WRITE)
		save_game.store_line("This is a test")
		save_game.close()	

	delete_file_with_extension("dat")
	delete_file_with_extension("json")


func delete_file_with_extension(ext):
	var dir = Directory.new()
	dir.open("user://")
	dir.list_dir_begin()
	while true:
		var file = dir.get_next()
		if file == "":
			break
		elif not file.begins_with(".") and file.right((file.length()-ext.length())) == ext:
			dir.remove(file)
	dir.list_dir_end()

I mean after exporting game i play it and do some saves after that i delete my game and export it again . The problem is that save files still exists in the user folder. They wasn’t deleted with the game.

sirAlexDev | 2020-08-22 09:37

Right OK.
I don’t know where data files are saved when exported, but I know they are defined here when in the editor. So if the user files are in a different directory, then they won’t get deleted.

deaton64 | 2020-08-22 09:50