# Own uploaded-file cleanup and build query links

> Verify default upload cleanup, autoclear(false), explicit clear and href query replacement.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: en
Content-SHA256: 5d7d9072b52f3dcc841f23b413c213e6a47f83eae4a521c8252f4f81d7f3f3c9
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

Uploaded temporary files normally disappear when the request is released. `$.autoclear(false)` keeps them for work that outlives the response: your application must then move or remove them. `$.clear()` schedules removal of removable uploads and empties `$.files`; disk deletion is asynchronous. Never store arbitrary paths supplied by a client for later deletion.

In 0.0.19 build query links with the public `QUERIFY()` helper and a copy of the query, as this recipe shows. It produces a query string, not a redirect or an absolute URL.

The retained-file map and cleanup endpoints below are a local fixture; an application should use scoped ownership and durable storage rather than expose these endpoints.

```js controllers/lifecycle.js
var paths = Object.create(null);
exports.install = function() {
	ROUTE('POST /upload/{mode}/ @upload <1mb', function($) {
		var file = $.files[0];
		if (!file) return $.invalid(400);
		var id = UID();
		paths[id] = file.path;
		if ($.params.mode === 'retain') $.autoclear(false);
		if ($.params.mode === 'clear') $.clear();
		$.json({ id: id, files: $.files.length });
	});
	ROUTE('GET /exists/{id}/', $ => $.json({ exists: !!paths[$.params.id] && F.Fs.existsSync(paths[$.params.id]) }));
	ROUTE('POST /cleanup/{id}/', function($) {
		var path = paths[$.params.id];
		if (path) { F.Fs.rmSync(path, { force: true }); delete paths[$.params.id]; }
		$.success();
	});
	ROUTE('GET /link/', function($) {
		var broken = false;
		try { $.href('page', '2'); } catch (e) { broken = e instanceof TypeError; }
		$.json({ broken: broken, link: QUERIFY('', Object.assign({}, $.query, { page: '2 & more' })) });
	});
};
```
