# Recommended approach for deleting generated pages

**URL:** <https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723>\
**Category:** support\
**Created:** [September 28, 2022, 6:05pm UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723 "2022-09-28T18:05:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrmanfitz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mrmanfitz/32/9177_2.png) [@mrmanfitz](https://discourse.gohugo.io/u/mrmanfitz)\
**Post date:** [September 28, 2022, 6:05pm UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/1 "2022-09-28T18:05:59Z")

</div>

Background:  
I build my site to a separate repo (GitHub Pages). If I delete a source markdown page, I want the corresponding built HTML page to be deleted during the build.

I’m familiar with `--cleanDestinationDir` ([hugo usage](https://gohugo.io/getting-started/usage/#test-installation)). The undesirable result of that option is that each commit of the built site deletes _every file_ and then _adds every file_. This seems a bit inefficient, but it also means I can never see a diff of my builds.

I’m also familiar with the default behavior around [draft, publishdate, and expirydate](https://gohugo.io/getting-started/usage/#draft-future-and-expired-content). However, I don’t see how `expirydate` is useful if the default behavior doesn’t remove the built page that has expired. In order for the `expirydate` behavior to be useful, don’t you have to also use the `--cleanDestinationDir` option? If so, the documentation should mention this.

Current Solution:

My current solution is to build to a temporary directory, then use `rsync` between the tmp build directory and the final repo destination. This ensures that a page that was not built to the tmp directory will actually get deleted (using the `--delete` option for rsync) from the final repo.

Problem:

However, now I’m dealing with `.gitignore` getting deleted and unwanted files in my build directory.

Can anyone comment on this general approach or share their solutions? Is there a better way to do this? I feel like I shouldn’t need rsync to make this work the way I want. Thoughts?

---

<div class="post-metadata">

**Author:** ![divinerites](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/divinerites/32/12625_2.png) [@divinerites](https://discourse.gohugo.io/u/divinerites)\
**Post date:** [September 28, 2022, 11:01pm UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/2 "2022-09-28T23:01:35Z")

</div>

> [@mrmanfitz](#):
>
> The undesirable result of that option is that each commit of the built site deletes _every file_ and then _adds every file_.

not from the source.

just `.gitignore` the `public` folder

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [September 28, 2022, 11:06pm UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/3 "2022-09-28T23:06:08Z")

</div>

Instead of source in one repo, and the built site in another repo, have you considered [this approach](https://gohugo.io/hosting-and-deployment/hosting-on-github/) instead? It builds to another branch (the gh-pages branch) instead of building to another repo.

---

<div class="post-metadata">

**Author:** ![mrmanfitz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mrmanfitz/32/9177_2.png) [@mrmanfitz](https://discourse.gohugo.io/u/mrmanfitz)\
**Post date:** [September 28, 2022, 11:38pm UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/4 "2022-09-28T23:38:04Z")

</div>

@divinerites I don’t build to `public`.

Here’s my build command:

```auto
hugo -v --ignoreCache --cleanDestinationDir --enableGitInfo -d /tmp/site/

```

Here’s my rsync command:

```bash
rsync -avh --filter='P .git' --filter='P CNAME' -exclude-from=sync_ignore.txt --backup --backup-dir=/tmp/site-bak/ /tmp/site/ /dst/site --delete

```

@jmooring No, not an option. My Hugo repo is private and the site repo is public.

---

<div class="post-metadata">

**Author:** ![divinerites](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/divinerites/32/12625_2.png) [@divinerites](https://discourse.gohugo.io/u/divinerites)\
**Post date:** [September 28, 2022, 11:41pm UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/5 "2022-09-28T23:41:46Z")

</div>

> [@mrmanfitz](#):
>
> I don’t build to `public`.

OK so .gitignore whatever you build to.

Git version control id for source code not for generated code

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [September 28, 2022, 11:41pm UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/6 "2022-09-28T23:41:59Z")

</div>

> [@mrmanfitz](#):
>
> My Hugo repo is private and the site repo is public.

You can keep the repo private.

---

<div class="post-metadata">

**Author:** ![mrmanfitz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mrmanfitz/32/9177_2.png) [@mrmanfitz](https://discourse.gohugo.io/u/mrmanfitz)\
**Post date:** [September 29, 2022, 12:18am UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/7 "2022-09-29T00:18:17Z")

</div>

The solution I’m hearing from you guys is to not care about the commit history of my generated site. That’s fair. I’ll consider that. However, I still use the commit review to spot-check that the build worked as expected. I guess I’m paranoid about that. I’ve been burned enough times by various anamolies in my own setup.

That said, the core of my question is really around the use of `--cleanDestinationDir` and `expirydate`. Is it accurate to say that `--cleanDestinationDir` is the recommended way to remove pages from the built site? From what I can tell, `expirydate` and `draft: true` on their own will not remove a previously built page.

---

<div class="post-metadata">

**Author:** ![nternetinspired](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/nternetinspired/32/19706_2.png) [@nternetinspired](https://discourse.gohugo.io/u/nternetinspired)\
**Post date:** [September 29, 2022, 12:28pm UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/8 "2022-09-29T12:28:56Z")

</div>

I have to agree with others here, checking in built assets seems completely wrong to me.

If you’re looking to prevent broken releases to your production server then set up deploy preview branches. I have a protected main branch that deploys to production, whilst my develop branch deploys on push. If that breaks, I don’t merge develop into my production branch. Git flow is your friend.

---

<div class="post-metadata">

**Author:** ![mrmanfitz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mrmanfitz/32/9177_2.png) [@mrmanfitz](https://discourse.gohugo.io/u/mrmanfitz)\
**Post date:** [September 29, 2022, 4:35pm UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/9 "2022-09-29T16:35:01Z")

</div>

@nternetinspired How do you use GitHub Pages without checking in built assets?

I’ll reiterate that my question is not really about .gitignore or where to build my site. My question is about how to delete a page from the built site.

> That said, the core of my question is really around the use of `--cleanDestinationDir` and `expirydate` . Is it accurate to say that `--cleanDestinationDir` is the recommended way to remove pages from the built site? From what I can tell, `expirydate` and `draft: true` on their own will not remove a previously built page.

---

<div class="post-metadata">

**Author:** ![divinerites](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/divinerites/32/12625_2.png) [@divinerites](https://discourse.gohugo.io/u/divinerites)\
**Post date:** [September 29, 2022, 5:50pm UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/10 "2022-09-29T17:50:55Z")

</div>

Deleting a generated page is done with the parameters you identify. I works flawlessly. Removed before generating a new one.

Your “problem” is that you want to use version control on your generated pages.

But you seems to keep on mixing the two question i guess.

`--cleanDestinationDir` is about the **generated** pages folder  
` expirydate` and `draft` are about the **source** content/pages and how they behave.

I will stop trying to give advices here.

---

<div class="post-metadata">

**Author:** ![nternetinspired](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/nternetinspired/32/19706_2.png) [@nternetinspired](https://discourse.gohugo.io/u/nternetinspired)\
**Post date:** [September 30, 2022, 8:45am UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/11 "2022-09-30T08:45:56Z")

</div>

I just nuke the entire build directory using a prebuild script defined in package.json

```auto
"scripts": {
    "build": "npm run build:webpack && npm run build:hugo",
    "build:webpack": "NODE_ENV=production webpack --config webpack.prod.js",
    "build:hugo": "hugo -d ./public --gc --minify --templateMetrics",
    "dev": "run-p dev:**",
    "dev:webpack": "TAILWIND_MODE=watch NODE_ENV=local webpack serve --config webpack.dev.js",
    "dev:hugo": "hugo -d ./public -w --buildDrafts --templateMetrics --templateMetricsHints --watch",
    "lint": "eslint src",
    "preview": "run-p preview:**",
    "preview:hugo": "npm run start:hugo -- -D -F",
    "prebuild": "rimraf public",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

```

Shiny fresh new site every time. Easy.

I don’t use GH Pages, but if I did I would do it the same way. Push my changed source to Github and have a GH action do the build.

---

<div class="post-metadata">

**Author:** ![mrmanfitz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mrmanfitz/32/9177_2.png) [@mrmanfitz](https://discourse.gohugo.io/u/mrmanfitz)\
**Post date:** [October 3, 2022, 6:55pm UTC](https://discourse.gohugo.io/t/recommended-approach-for-deleting-generated-pages/40723/12 "2022-10-03T18:55:46Z")

</div>

Thanks for confirming the best practice and expected usage. [Docs PR](https://github.com/gohugoio/hugoDocs/pull/1831) created.
